﻿using System;
using System.IO;
using System.Collections;
using System.Data;
using System.Text;
using System.Web;
using BP.DA;
using BP.Sys;
using BP.Tools;
using BP.Web;
using BP.WF.Template;
using BP.En;
using ICSharpCode.SharpZipLib.Zip;
using BP.Difference;
using System.Drawing;
using BP.Sys.FrmUI;
using BP.WF.Template.SFlow;
using BP.WF.Template.Frm;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using LitJson;
using System.Net;
using DataType = BP.DA.DataType;
using System.Linq;
using System.Threading;
using Log = BP.DA.Log;
using BP.Pub;


namespace BP.WF.HttpHandler
{
    /// <summary>
    /// 表单功能界面
    /// </summary>
    public class WF_CCForm : DirectoryPageBase
    {
        public string FrmGL_InitData()
        {
            if (BP.DA.DataType.IsNullOrEmpty(this.FrmID) || BP.DA.DataType.IsNullOrEmpty(this.RefNo))
            {
                return "err@参数不能为空";
            }
            string pk = this.FrmID + "_" + this.RefNo + "_FieldPopShowDtl";
            MapExt me = new MapExt(pk);
            string sql = me.Doc;
            if (BP.DA.DataType.IsNullOrEmpty(sql))
            {
                return "err@请先配置数据源sql";

            }
            GEEntity en = new GEEntity(this.FrmID, this.PKVal);

            sql = Glo.DealSQLExp(sql, en, null);

            DataTable dt = DBAccess.RunSQLReturnTable(sql);
            dt.TableName = "Data";
            DataTable dtAttrs = new DataTable("Attrs");
            dtAttrs.Columns.Add("Key", typeof(string));
            dtAttrs.Columns.Add("Name", typeof(string));
            dtAttrs.Columns.Add("IsShow", typeof(bool));
            dtAttrs.Columns.Add("DataType", typeof(int));
            foreach (DataColumn dc in dt.Columns)
            {
                DataRow dataRow = dtAttrs.NewRow();
                dataRow["Key"] = dc.ColumnName;
                dataRow["Name"] = dc.ColumnName;
                dataRow["IsShow"] = true;
                Type dataType = dc.DataType;
                if (dataType.Name.Contains("String"))
                {
                    dataRow["DataType"] = BP.DA.DataType.AppString;
                }
                else if (dataType.Name.Contains("Int32"))
                {
                    dataRow["DataType"] = BP.DA.DataType.AppInt;
                }
                else if (dataType.Name.Contains("Boolean"))
                {
                    dataRow["DataType"] = BP.DA.DataType.AppBoolean;
                }
                else if (dataType.Name.Contains("Decimal"))
                {
                    dataRow["DataType"] = BP.DA.DataType.AppDouble;
                }
                else
                {
                    dataRow["DataType"] = BP.DA.DataType.AppString;
                }

                dtAttrs.Rows.Add(dataRow);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ds.Tables.Add(dtAttrs);

            return BP.Tools.Json.ToJson(ds);
        }


        /// <summary>
        /// 根据拼音
        /// </summary>
        /// <returns></returns>
        public string FrmAPI_GenerMyDataType()
        {
            List<string> arrayList = new List<string>();
            arrayList.Add(this.GetRequestVal("name"));
            return BP.Sys.CCFormAPI.GenerDataTypeByCHName(arrayList);
        }
        /// <summary>
        /// 根据中文字段名获得
        /// </summary>
        /// <returns></returns>
        public string FrmAPI_GenerFieldEndInfo()
        {
            ArrayList arrayList = new ArrayList();
            arrayList.Add(this.GetRequestVal("name"));
            return BP.Sys.CCFormAPI.GenerFildEndInfoByCHName(arrayList);
        }

        #region 多附件.
        /// <summary>
        /// 获得数据
        /// </summary>
        /// <returns></returns>
        public string Ath_Init()
        {
            try
            {
                DataSet ds = new DataSet();

                FrmAttachment athDesc = this.GenerAthDesc();

                //查询出来数据实体.
                string pkVal = this.GetRequestVal("RefOID");
                if (DataType.IsNullOrEmpty(pkVal) == true)
                    pkVal = this.GetRequestVal("OID");
                if (DataType.IsNullOrEmpty(pkVal) == true)
                    pkVal = this.WorkID.ToString();
                DataTable athDt = null;
                bool ishistoryData = this.GetRequestValBoolen("IshistoryData");
                if (ishistoryData == false)
                {
                    BP.Sys.FrmAttachmentDBs dbs = BP.WF.CCFormAPI.GenerFrmAttachmentDBs(athDesc, pkVal,
                    this.FK_FrmAttachment, this.WorkID, this.FID, this.PWorkID, true, this.NodeID, this.FrmID);
                    athDt = dbs.ToDataTableField("DBAths");
                }
                else
                {
                    //获取历史数据
                    String trackID = this.GetRequestVal("TrackID");
                    if (DataType.IsNullOrEmpty(trackID) == false)
                    {
                        //根据TrackID从Sys_FrmDBVer表中获取历史数据
                        FrmDBVer ver = new FrmDBVer();
                        int i = ver.Retrieve(FrmDBVerAttr.TrackID, trackID);
                        if (i == 1)
                        {
                            String athStr = DBAccess.GetBigTextFromDB("Sys_FrmDBVer", "MyPK", ver.MyPK, "FrmAthDB");
                            DataSet athDS = BP.Tools.Json.ToDataSet(athStr);
                            athDt = athDS.Tables[athDesc.NoOfObj];
                            athDt.TableName = "DBAths";
                        }
                    }
                    else
                    {
                        BP.Sys.FrmAttachmentDBs dbs = BP.WF.CCFormAPI.GenerFrmAttachmentDBs(athDesc, pkVal,
                                           this.FK_FrmAttachment, this.WorkID, this.FID, this.PWorkID, true, this.NodeID, this.FrmID); athDt = dbs.ToDataTableField("DBAths");
                        athDt = dbs.ToDataTableField("DBAths");
                    }
                }
                #region 如果图片显示.(先不考虑.)
                if (athDesc.FileShowWay == FileShowWay.Pict)
                {
                    /* 如果是图片轮播，就在这里根据数据输出轮播的html代码.*/
                    if (athDt.Rows.Count == 0 && athDesc.ItIsUpload == true)
                    {
                        /*没有数据并且，可以上传,就转到上传的界面上去.*/
                        return "url@AthImg.htm?1=1" + this.RequestParas;
                    }
                }
                #endregion 如果图片显示.

                /*#region 执行装载模版. athDesc.IsWoEnableTemplete == true
                if (athDt.Rows.Count == 0 && 1 == 2)
                {
                    *//*如果数量为0,就检查一下是否有模版如果有就加载模版文件.*//*
                    string templetePath = BP.Difference.SystemConfig.PathOfDataUser + "AthTemplete/" + athDesc.NoOfObj.Trim();
                    if (Directory.Exists(templetePath) == false)
                        Directory.CreateDirectory(templetePath);

                    *//*有模版文件夹*//*
                    DirectoryInfo mydir = new DirectoryInfo(templetePath);
                    FileInfo[] fls = mydir.GetFiles();
                    if (fls.Length == 0)
                        throw new Exception("@流程设计错误，该多附件启用了模版组件，模版目录:" + templetePath + "里没有模版文件.");

                    foreach (FileInfo fl in fls)
                    {
                        if (System.IO.Directory.Exists(athDesc.SaveTo) == false)
                            System.IO.Directory.CreateDirectory(athDesc.SaveTo);

                        int oid = DBAccess.GenerOID();
                        string saveTo = athDesc.SaveTo + "/" + oid + "." + fl.Name.Substring(fl.Name.LastIndexOf('.') + 1);
                        if (saveTo.Contains("@") == true || saveTo.Contains("*") == true)
                        {
                            *//*如果有变量*//*
                            saveTo = saveTo.Replace("*", "@");
                            if (saveTo.Contains("@") && this.NodeID != 0)
                            {
                                *//*如果包含 @ *//*
                                BP.WF.Flow flow = new BP.WF.Flow(this.FlowNo);
                                BP.WF.GERpt myen = flow.HisGERpt;
                                myen.OID = this.WorkID;
                                myen.RetrieveFromDBSources();
                                saveTo = BP.WF.Glo.DealExp(saveTo, myen, null);
                            }
                            if (saveTo.Contains("@") == true)
                                throw new Exception("@路径配置错误,变量没有被正确的替换下来." + saveTo);
                        }
                        fl.CopyTo(saveTo);

                        FileInfo info = new FileInfo(saveTo);
                        FrmAttachmentDB dbUpload = new FrmAttachmentDB();

                        //dbUpload.CheckPhysicsTable();
                        dbUpload.setMyPK(athDesc.FrmID + oid.ToString());
                        dbUpload.NodeID = this.NodeID;
                        dbUpload.FrmID = athDesc.FrmID;

                        dbUpload.FK_FrmAttachment = this.FK_FrmAttachment;

                        if (athDesc.AthUploadWay == AthUploadWay.Inherit)
                        {
                            *//*如果是继承，就让他保持本地的PK. *//*
                            dbUpload.RefPKVal = this.PKVal.ToString();
                        }

                        if (athDesc.AthUploadWay == AthUploadWay.Interwork)
                        {
                            *//*如果是协同，就让他是PWorkID. *//*
                            Paras ps = new Paras();
                            ps.SQL = "SELECT PWorkID FROM WF_GenerWorkFlow WHERE WorkID=" + BP.Difference.SystemConfig.AppCenterDBVarStr + "WorkID";
                            ps.Add("WorkID", this.PKVal);
                            string pWorkID = DBAccess.RunSQLReturnValInt(ps, 0).ToString();
                            if (pWorkID == null || pWorkID == "0")
                                pWorkID = this.PKVal;
                            dbUpload.RefPKVal = pWorkID;
                        }

                        dbUpload.FrmID = athDesc.FrmID;
                        dbUpload.FK_FrmAttachment = this.FK_FrmAttachment;

                        dbUpload.FileExts = info.Extension;
                        dbUpload.FileFullName = saveTo;
                        dbUpload.FileName = fl.Name;
                        dbUpload.FileSize = (float)info.Length;

                        dbUpload.RDT = DataType.CurrentDateTime;
                        dbUpload.Rec = BP.Web.WebUser.No;
                        dbUpload.RecName = BP.Web.WebUser.Name;
                        dbUpload.DeptNo = WebUser.DeptNo;
                        dbUpload.DeptName = WebUser.DeptName;

                        dbUpload.Insert();

                        dbs.AddEntity(dbUpload);
                    }
                }
                #endregion 执行装载模版.*/

                #region 处理权限问题.
                // 处理权限问题, 有可能当前节点是可以上传或者删除，但是当前节点上不能让此人执行工作。
                // bool isDel = athDesc.IsDeleteInt == 0 ? false : true;
                bool isDel = athDesc.HisDeleteWay == AthDeleteWay.None ? false : true;
                bool isUpdate = athDesc.ItIsUpload;
                //if (isDel == true || isUpdate == true)
                //{
                //    if (this.WorkID != 0
                //        && DataType.IsNullOrEmpty(this.FlowNo) == false
                //        && this.NodeID != 0)
                //    {
                //        isDel = BP.WF.Dev2Interface.Flow_IsCanDoCurrentWork(this.FlowNo, this.NodeID, this.WorkID, WebUser.No);
                //        if (isDel == false)
                //            isUpdate = false;
                //    }
                //}
                athDesc.ItIsUpload = isUpdate;
                //athDesc.HisDeleteWay = AthDeleteWay.DelAll; 
                #endregion 处理权限问题.

                string sort = athDesc.Sort.Trim();
                if (sort.Contains("SELECT") == true || sort.Contains("select") == true)
                {
                    string sql = BP.WF.Glo.DealExp(sort, null, null);
                    DataTable dt = DBAccess.RunSQLReturnTable(sql);
                    string strs = "";
                    foreach (DataRow dr in dt.Rows)
                    {
                        strs += dr[0] + ",";
                    }
                    athDesc.Sort = strs;
                }


                //增加附件描述.
                ds.Tables.Add(athDesc.ToDataTableField("AthDesc"));

                //增加附件.
                ds.Tables.Add(athDt);

                //返回.
                return BP.Tools.Json.ToJson(ds);
            }
            catch (Exception ex)
            {
                return "err@" + ex.Message;
            }
        }
        #endregion 多附件.

        #region HanderMapExt
        /// <summary>
        /// 扩展处理.
        /// </summary>
        /// <returns></returns>
        public string HandlerMapExt()
        {
            string fk_mapExt = this.GetRequestVal("FK_MapExt").ToString();
            string oid = this.GetRequestVal("OID");

            string kvs = this.GetRequestVal("KVs");

            BP.Sys.MapExt me = new BP.Sys.MapExt(fk_mapExt);
            DataTable dt = null;
            string sql = "";
            string key = this.GetRequestVal("Key");
            key = key.Trim();
            key = key.Replace("'", ""); //去掉单引号.

            string dbsrc = me.DBSrcNo;
            SFDBSrc sfdb = null;
            if (DataType.IsNullOrEmpty(dbsrc) == false && dbsrc.Equals("local") == false)
                sfdb = new SFDBSrc(dbsrc);
            string dbType = me.DBType; //0 执行SQL 1 执行URL
            string requestMesthod = me.Tag3; // Get ,Post
            string questbody = me.Tag4; //body
            string type = "";

            switch (me.ExtType)
            {
                case BP.Sys.MapExtXmlList.FullData: // 填充其他控件.
                    switch (this.GetRequestVal("DoTypeExt"))
                    {
                        case "ReqCtrl":
                            string requestbody = me.Tag4; //body
                            requestbody = requestbody.Replace("'", "\"");
                            requestbody = DealSQL(requestbody, key);
                            requestbody = requestbody.Replace("@fulltype", "0");
                            string urlCtrl = DealSQL(me.Doc, key);
                            string strCtrl = PubGlo.HttpPostConnect(urlCtrl, requestbody, "POST", true);
                            JObject jsonCtrl = strCtrl.ToJObject();
                            //code=0，表示请求成功，否则失败
                            if (jsonCtrl["code"] == null)
                                return "err@执行URL返回结果失败";
                            if (jsonCtrl["code"].ToString().Equals("0") == false)
                                return "err@执行URL返回结果失败";
                            if (jsonCtrl["msg"] != null)
                                return "err@" + jsonCtrl["msg"];

                            string ctrlData = jsonCtrl["data"].ToString();
                            return ctrlData;
                            break;
                        case "ReqDDLFullList":
                            type = me.GetValStringByKey("Tag6");
                            if (type.Equals("1") && dbType.Equals("1"))
                            {
                                string url = DealSQL(me.Tag, key);
                                string result = "";
                                if (requestMesthod.ToLower().Equals("get"))
                                    result = DataType.ReadURLContext(url, 9000);
                                else
                                {
                                    questbody = questbody.Replace("'", "\"");
                                    questbody = DealSQL(questbody, key);
                                    questbody = questbody.Replace("@fulltype", "1");
                                    result = PubGlo.HttpPostConnect(url, questbody, "POST", true);
                                }
                                if (DataType.IsNullOrEmpty(result) == true)
                                    return "err@请求失败";
                                //数据序列化
                                JObject jsonData = result.ToJObject();
                                //code=0，表示请求成功，否则失败
                                if (jsonData["code"].ToString().Equals("0") == false)
                                    return "err@执行URL返回结果失败";
                                string data = jsonData["data"].ToString();
                                JToken jToken = JToken.Parse(data);
                                DataSet ds = new DataSet();
                                if (jToken.Type == JTokenType.Object)
                                {
                                    JObject jsonItem = (JObject)jToken;
                                    System.Collections.Generic.IEnumerable<JProperty> properties = jsonItem.Properties();
                                    foreach (JProperty item in properties)
                                    {
                                        //判断值是不是集合
                                        if (item.Value.Type == JTokenType.Array)
                                        {
                                            JArray arr = (JArray)item.Value;
                                            if (arr.Count == 0)
                                                continue;
                                            dt = BP.Tools.Json.ConvertToDataTable(arr);
                                            dt.TableName = item.Name;
                                            ds.Tables.Add(dt);
                                        }

                                    }
                                }
                                return BP.Tools.Json.ToJson(ds);
                            }
                            if (type.Equals("0") && dbType.Equals("1"))
                            {
                                DataSet ds = new DataSet();
                                string[] strs = me.Tag.Split('$');
                                foreach (string str in strs)
                                {
                                    if (DataType.IsNullOrEmpty(str) == true)
                                        continue;
                                    string[] ss = str.Split(':');
                                    if (ss.Length != 2)
                                        continue;
                                    string keyOfEn = ss[0];
                                    string url = str.Replace(keyOfEn + ":", "");
                                    url = DealSQL(url, key);
                                    string result = "";
                                    if (requestMesthod.ToLower().Equals("get"))
                                        result = DataType.ReadURLContext(url, 9000);
                                    else
                                    {
                                        questbody = questbody.Replace("'", "\"");
                                        questbody = DealSQL(questbody, key);
                                        questbody = questbody.Replace("@fulltype", "1");
                                        result = PubGlo.HttpPostConnect(url, questbody, "POST", true);
                                    }
                                    if (DataType.IsNullOrEmpty(result) == true)
                                        return "err@请求失败";
                                    //数据序列化
                                    JObject jsonData = result.ToJObject();
                                    //code=0，表示请求成功，否则失败
                                    if (jsonData["code"].ToString().Equals("0") == false)
                                        return "err@执行URL返回结果失败";
                                    string data = jsonData["data"].ToString();
                                    if (DataType.IsNullOrEmpty(data) == true)
                                        continue;
                                    dt = BP.Tools.Json.ToDataTable(data);
                                    dt.TableName = keyOfEn;
                                    ds.Tables.Add(dt);
                                }
                                return BP.Tools.Json.ToJson(ds);
                            }
                            break;
                        case "ReqDtlFullList":
                            /* 获取填充的从表集合. */
                            DataTable dtDtl = new DataTable("Head");
                            dtDtl.Columns.Add("Dtl", typeof(string));
                            type = me.GetValStringByKey("Tag5");
                            if (type.Equals("1") && dbType.Equals("1"))
                            {
                                string url = DealSQL(me.Tag1, key);
                                string result = "";
                                if (requestMesthod.ToLower().Equals("get"))
                                    result = DataType.ReadURLContext(url, 9000);
                                else
                                {
                                    questbody = questbody.Replace("'", "\"");
                                    questbody = DealSQL(questbody, key);
                                    questbody = questbody.Replace("@fulltype", "1");
                                    result = PubGlo.HttpPostConnect(url, questbody, "POST", true);
                                }
                                if (DataType.IsNullOrEmpty(result) == true)
                                    return "err@请求失败";
                                //数据序列化
                                JObject jsonData = result.ToJObject();
                                //code=0，表示请求成功，否则失败
                                if (jsonData["code"].ToString().Equals("0") == false)
                                    return "err@执行URL返回结果失败";
                                string data = jsonData["data"].ToString();
                                JToken jToken = JToken.Parse(data);
                                if (jToken.Type == JTokenType.Object)
                                {
                                    JObject jsonItem = (JObject)jToken;
                                    System.Collections.Generic.IEnumerable<JProperty> properties = jsonItem.Properties();
                                    foreach (JProperty item in properties)
                                    {
                                        //判断值是不是集合
                                        if (item.Value.Type == JTokenType.Array)
                                        {
                                            JArray arr = (JArray)item.Value;
                                            if (arr.Count == 0)
                                                continue;
                                            dt = BP.Tools.Json.ConvertToDataTable(arr);
                                            MapDtl dtl = new MapDtl();
                                            dtl.No = item.Name;
                                            int i = dtl.RetrieveFromDBSources();
                                            if (i == 0)
                                                continue;

                                            try
                                            {
                                                DBAccess.RunSQL("DELETE FROM " + dtl.PTable + " WHERE RefPK=" + oid);
                                            }
                                            catch (Exception ex)
                                            {
                                                BP.Sys.GEDtl mydtl = new GEDtl(dtl.No);
                                                mydtl.CheckPhysicsTable();
                                            }

                                            foreach (DataRow dr in dt.Rows)
                                            {
                                                BP.Sys.GEDtl mydtl = new GEDtl(dtl.No);
                                                MapAttrs attrs = dtl.MapAttrs;
                                                foreach (DataColumn dc in dt.Columns)
                                                {
                                                    //填充从表里的枚举
                                                    MapAttr attr = null;
                                                    Entity ent = dtl.MapAttrs.GetEntityByKey(dtl.No + "_" + dc.ColumnName);
                                                    if (ent == null)
                                                        continue;
                                                    attr = ent as MapAttr;
                                                    string uiBindKey = attr.UIBindKey;
                                                    string mysql = "";

                                                    if (uiBindKey != "" && uiBindKey != null)
                                                    {
                                                        //根据枚举value填充
                                                        if (dr[dc.ColumnName].GetType() == typeof(string))
                                                        {
                                                            if (dr[dc.ColumnName].ToString() == "" || dr[dc.ColumnName].ToString() == " ")
                                                            {
                                                                mydtl.SetValByKey(dc.ColumnName, 0);
                                                                continue;
                                                            }
                                                            else
                                                            {
                                                                try
                                                                {
                                                                    mysql = "SELECT IntKey AS No FROM  " + BP.Sys.Base.Glo.SysEnum() + " WHERE EnumKey='" + uiBindKey + "' and Lab ='" + dr[dc.ColumnName].ToString() + "' ORDER BY IntKey ";
                                                                    int keyEum = DBAccess.RunSQLReturnValInt(mysql);
                                                                    mydtl.SetValByKey(dc.ColumnName, keyEum);
                                                                    continue;
                                                                }
                                                                catch (Exception ex)
                                                                {
                                                                    mydtl.SetValByKey(dc.ColumnName, 0);
                                                                    continue;
                                                                }
                                                            }
                                                        }
                                                        if (dr[dc.ColumnName].GetType() == typeof(int))
                                                        {
                                                            mydtl.SetValByKey(dc.ColumnName, dr[dc.ColumnName]);
                                                        }
                                                    }
                                                    mydtl.SetValByKey(dc.ColumnName, dr[dc.ColumnName].ToString());
                                                }
                                                mydtl.RefPKInt = int.Parse(oid);
                                                if (mydtl.OID > 100)
                                                {
                                                    mydtl.InsertAsOID(mydtl.OID);
                                                }
                                                else
                                                {
                                                    mydtl.OID = 0;
                                                    mydtl.InsertAsOID(DBAccess.GenerOID(dtl.PTable));
                                                }

                                            }
                                            DataRow drRe = dtDtl.NewRow();
                                            drRe[0] = dtl.No;
                                            dtDtl.Rows.Add(drRe);
                                        }

                                    }
                                }
                                return BP.Tools.Json.ToJson(dtDtl);
                            }
                            string[] strsDtl = me.Tag1.Split('$');
                            foreach (string str in strsDtl)
                            {
                                if (DataType.IsNullOrEmpty(str))
                                    continue;

                                string[] ss = str.Split(':');
                                string fk_dtl = ss[0];
                                MapDtl dtl = new MapDtl(fk_dtl);
                                string mysql = str.Replace(dtl.No + ":", "");
                                if (mysql == "" || mysql == null)
                                    continue;
                                string dtlKey = this.GetRequestVal("DtlKey");
                                if (dtlKey == null)
                                    dtlKey = key;
                                if (dtlKey.IndexOf(",") != -1)
                                    dtlKey = "'" + dtlKey.Replace(",", "','") + "'";
                                mysql = DealSQL(mysql, dtlKey);
                                if (mysql.Length <= 10)
                                    continue;
                                if (mysql.Contains("@"))
                                    return "请求的语句" + mysql + "还有未替换的@符号";

                                GEDtls dtls = new GEDtls(fk_dtl);

                                DataTable dtDtlFull = null;
                                if (me.DBType.Equals("0"))
                                {
                                    try
                                    {
                                        if (sfdb != null)
                                            dtDtlFull = sfdb.RunSQLReturnTable(mysql);
                                        else
                                            dtDtlFull = DBAccess.RunSQLReturnTable(mysql);
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception("err@执行填充从表出现错误,[" + dtl.No + " - " + dtl.Name + "]设置的SQL" + mysql);
                                    }
                                }
                                if (me.DBType.Equals("1"))
                                {
                                    System.Text.Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
                                    string json = DataType.ReadURLContext(mysql, 8000, encode);
                                    if (DataType.IsNullOrEmpty(json) == true)
                                        return "err@执行URL没有返回结果值";

                                    dtDtlFull = BP.Tools.Json.ToDataTable(json);
                                }

                                try
                                {
                                    DBAccess.RunSQL("DELETE FROM " + dtl.PTable + " WHERE RefPK=" + oid);
                                }
                                catch (Exception ex)
                                {
                                    BP.Sys.GEDtl mydtl = new GEDtl(fk_dtl);
                                    mydtl.CheckPhysicsTable();
                                }

                                foreach (DataRow dr in dtDtlFull.Rows)
                                {
                                    BP.Sys.GEDtl mydtl = new GEDtl(fk_dtl);
                                    //  mydtl.OID = dtls.Count + 1;
                                    dtls.AddEntity(mydtl);
                                    int index = dtDtlFull.Rows.IndexOf(dr);
                                    foreach (DataColumn dc in dtDtlFull.Columns)
                                    {

                                        mydtl.SetValByKey(dc.ColumnName, dr[dc.ColumnName].ToString());
                                    }
                                    mydtl.RefPKInt = int.Parse(oid);
                                    if (mydtl.OID > 100)
                                    {
                                        mydtl.InsertAsOID(mydtl.OID);
                                    }
                                    else
                                    {
                                        mydtl.SetValByKey("Idx", index);
                                        mydtl.OID = 0;
                                        mydtl.InsertAsOID(DBAccess.GenerOID(dtl.PTable));
                                    }

                                }
                                DataRow drRe = dtDtl.NewRow();
                                drRe[0] = fk_dtl;
                                dtDtl.Rows.Add(drRe);
                            }
                            return BP.Tools.Json.ToJson(dtDtl);
                            break;
                    }
                    return "";
                default:
                    return "err@没有解析的标记" + me.ExtType;
            }

            return "err@没有解析的标记" + me.ExtType;
        }
        /// <summary>
        /// 处理sql.
        /// </summary>
        /// <param name="sql">要执行的sql</param>
        /// <param name="key">关键字值</param>
        /// <returns>执行sql返回的json</returns>
        private string DealSQL(string sql, string key)
        {
            sql = sql.Replace("@Key", key);
            sql = sql.Replace("@key", key);
            sql = sql.Replace("@Val", key);
            sql = sql.Replace("@val", key);
            sql = sql.Replace("@val", key);

            sql = sql.Replace("\n", "");
            sql = sql.Replace("\n", "");
            sql = sql.Replace("\n", "");
            sql = sql.Replace("\n", "");
            sql = sql.Replace("\n", "");


            sql = sql.Replace("@WebUser.No", WebUser.No);
            sql = sql.Replace("@WebUser.Name", WebUser.Name);
            sql = sql.Replace("@WebUser.FK_Dept", WebUser.DeptNo);
            sql = sql.Replace("@WebUser.DeptNo", WebUser.DeptNo);


            string oid = this.GetRequestVal("OID");
            if (oid != null)
                sql = sql.Replace("@OID", oid);

            string kvs = this.GetRequestVal("KVs");

            if (DataType.IsNullOrEmpty(kvs) == false && sql.Contains("@") == true)
            {
                string[] strs = kvs.Split('~');
                foreach (string s in strs)
                {
                    if (DataType.IsNullOrEmpty(s)
                        || s.Contains("=") == false)
                        continue;

                    string[] mykv = s.Split('=');
                    string sql2 = sql.Split('@')[1];
                    if (mykv[0].Contains(sql2))
                    {
                        sql = sql.Replace("@" + mykv[0] + "@", mykv[1]);
                    }
                    if (sql.Contains("@") == false)
                        break;
                }
            }

            if (sql.Contains("@") == true)
            {
                foreach (string mykey in HttpContextHelper.RequestParamKeys)
                {
                    sql = sql.Replace("@" + mykey, HttpContextHelper.RequestParams(mykey));
                }
            }

            dealSQL = sql;
            return sql;
        }
        private string dealSQL = "";

        #endregion HanderMapExt

        #region 执行父类的重写方法.

        /// <summary>
        /// 构造函数
        /// </summary>
        public WF_CCForm()
        {
        }
        /// <summary>
        /// 默认执行的方法
        /// </summary>
        /// <returns></returns>
        protected override string DoDefaultMethod()
        {
            //找不不到标记就抛出异常.
            throw new Exception("@标记[" + this.DoType + "]，没有找到.@原始URL:" + HttpContextHelper.RequestRawUrl);
        }
        #endregion 执行父类的重写方法.

        #region frm.htm 主表.
        /// <summary>
        /// 执行数据初始化
        /// </summary>
        /// <returns></returns>
        public string Frm_Init()
        {
            bool IsMobile = GetRequestValBoolen("IsMobile");
            if (this.GetRequestVal("IsTest") != null)
            {
                MapData mymd = new MapData(this.EnsName);
                mymd.RepairMap();
                SystemConfig.DoClearCache();
            }

            MapData md = new MapData(this.EnsName);

            #region 判断是否是返回的URL.
            if (md.HisFrmType == FrmType.Url)
            {
                string no = this.GetRequestVal("NO");
                string urlParas = "OID=" + this.RefOID + "&NO=" + no + "&WorkID=" + this.WorkID + "&FK_Node=" + this.NodeID + "&UserNo=" + WebUser.No + "&Token=" + this.SID + "&FID=" + this.FID + "&PWorkID=" + this.WorkID;

                string url = "";
                /*如果是URL.*/
                if (md.UrlExt.Contains("?") == true)
                    url = md.UrlExt + "&" + urlParas;
                else
                    url = md.UrlExt + "?" + urlParas;

                return "url@" + url;
            }

            if (md.HisFrmType == FrmType.Entity)
            {
                string no = this.GetRequestVal("NO");
                string urlParas = "OID=" + this.RefOID + "&NO=" + no + "&WorkID=" + this.WorkID + "&FK_Node=" + this.NodeID + "&UserNo=" + WebUser.No + "&Token=" + this.SID;

                BP.En.Entities ens = BP.En.ClassFactory.GetEns(md.PTable);

                BP.En.Entity en = ens.GetNewEntity;

                if (en.ItIsOIDEntity == true)
                {
                    BP.En.EntityOID enOID = en as BP.En.EntityOID;

                    if (enOID == null)
                    {
                        return "err@系统错误，无法将" + md.PTable + "转化成 BP.En.EntityOID.";
                    }

                    enOID.SetValByKey("OID", this.WorkID);

                    if (en.RetrieveFromDBSources() == 0)
                    {
                        foreach (string key in HttpContextHelper.RequestParamKeys)
                        {
                            enOID.SetValByKey(key, HttpContextHelper.RequestParams(key));
                        }
                        enOID.SetValByKey("OID", this.WorkID);

                        enOID.InsertAsOID(this.WorkID);
                    }
                }
                return "url@../Comm/En.htm?EnName=" + md.PTable + "&PKVal=" + this.WorkID;
            }

            //if (md.HisFrmType == FrmType.VSTOForExcel && this.GetRequestVal("IsFreeFrm") == null)
            //{
            //    string url = "FrmVSTO.htm?1=1" + this.RequestParasOfAll;
            //    url = url.Replace("&&", "&");
            //    return "url@" + url;
            //}


            if (md.HisFrmType == FrmType.WordFrm || md.HisFrmType == FrmType.WPSFrm)
            {
                string no = this.GetRequestVal("NO");
                string urlParas = "OID=" + this.RefOID + "&NO=" + no + "&WorkID=" + this.WorkID + "&FK_Node=" + this.NodeID + "&UserNo=" + WebUser.No + "&Token=" + this.SID + "&FK_MapData=" + this.FrmID + "&OIDPKVal=" + this.OID + "&FID=" + this.FID + "&FK_Flow=" + this.FlowNo;
                /*如果是URL.*/
                string requestParas = this.RequestParasOfAll;
                string[] parasArrary = this.RequestParasOfAll.Split('&');
                foreach (string str in parasArrary)
                {
                    if (DataType.IsNullOrEmpty(str) || str.Contains("=") == false)
                        continue;
                    string[] kvs = str.Split('=');
                    if (urlParas.Contains(kvs[0]))
                        continue;
                    urlParas += "&" + kvs[0] + "=" + kvs[1];
                }


                string frm = "FrmWord";
                if (md.HisFrmType == FrmType.WPSFrm) frm = "WpsFrm";

                if (md.UrlExt.Contains("?") == true)
                    return "url@" + frm + ".htm?1=2" + "&" + urlParas;
                else
                    return "url@" + frm + ".htm" + "?" + urlParas;
            }

            if (md.HisFrmType == FrmType.ExcelFrm)
                return "url@FrmExcel.htm?1=2" + this.RequestParasOfAll;

            #endregion 判断是否是返回的URL.

            //处理参数.
            string paras = this.RequestParasOfAll;
            paras = paras.Replace("&DoType=Frm_Init", "");
            BP.WF.Template.FrmNode fn = null;

            #region 流程的独立运行的表单.
            if (this.NodeID != 0 && this.NodeID != 999999)
            {
                fn = new FrmNode(this.NodeID, this.FrmID);
                if (fn.FrmSln == FrmSln.Readonly)
                    paras = paras + "&IsReadonly=1";

                if (fn != null && fn.WhoIsPK != WhoIsPK.OID)
                {
                    //太爷孙关系
                    if (fn.WhoIsPK == WhoIsPK.P3WorkID)
                    {
                        //根据PWorkID 获取P3WorkID
                        string sql = "Select PWorkID From WF_GenerWorkFlow Where WorkID=(Select PWorkID From WF_GenerWorkFlow Where WorkID=" + this.PWorkID + ")";
                        string p3workID = DBAccess.RunSQLReturnString(sql);
                        if (DataType.IsNullOrEmpty(p3workID) == true || p3workID == "0")
                            throw new Exception("err@不存在太爷孙流程关系，请联系管理员检查流程设计是否正确");

                        Int64 workID = Int64.Parse(p3workID);
                        paras = paras.Replace("&OID=" + this.WorkID, "&OID=" + workID);
                        paras = paras.Replace("&WorkID=" + this.WorkID, "&WorkID=" + workID);
                        paras = paras.Replace("&PKVal=" + this.WorkID, "&PKVal=" + workID);
                    }

                    if (fn.WhoIsPK == WhoIsPK.P2WorkID)
                    {
                        //根据PWorkID 获取PPWorkID
                        GenerWorkFlow gwf = new GenerWorkFlow(this.PWorkID);
                        if (gwf != null && gwf.PWorkID != 0)
                        {
                            paras = paras.Replace("&OID=" + this.WorkID, "&OID=" + gwf.PWorkID);
                            paras = paras.Replace("&WorkID=" + this.WorkID, "&WorkID=" + gwf.PWorkID);
                            paras = paras.Replace("&PKVal=" + this.WorkID, "&PKVal=" + gwf.PWorkID);
                        }
                        else
                        {
                            throw new Exception("err@不存在爷孙流程关系，请联系管理员检查流程设计是否正确");
                        }
                    }

                    if (fn.WhoIsPK == WhoIsPK.PWorkID)
                    {
                        paras = paras.Replace("&OID=" + this.WorkID, "&OID=" + this.PWorkID);
                        paras = paras.Replace("&WorkID=" + this.WorkID, "&WorkID=" + this.PWorkID);
                        paras = paras.Replace("&PKVal=" + this.WorkID, "&PKVal=" + this.PWorkID);
                    }

                    if (fn.WhoIsPK == WhoIsPK.FID)
                    {
                        paras = paras.Replace("&OID=" + this.WorkID, "&OID=" + this.FID);
                        paras = paras.Replace("&WorkID=" + this.WorkID, "&WorkID=" + this.FID);
                        paras = paras.Replace("&PKVal=" + this.WorkID, "&PKVal=" + this.FID);
                    }

                    if ((this.GetRequestVal("ShowFrmType") != null && this.GetRequestVal("ShowFrmType").Equals("FrmFool") == true)
                        || md.HisFrmType == FrmType.Develop || md.HisFrmType == FrmType.FoolForm)
                    {
                        if (IsMobile == true)
                            return "url@../FrmView.htm?1=2" + paras;

                        if (this.GetRequestValBoolen("Readonly") == true || this.GetRequestValBoolen("IsEdit") == false)
                            return "url@FrmGener.htm?1=2" + paras;
                        else
                            return "url@FrmGener.htm?1=2" + paras;
                    }

                    if (md.HisFrmType == FrmType.VSTOForExcel || md.HisFrmType == FrmType.ExcelFrm)
                    {
                        if (this.GetRequestValBoolen("Readonly") == true || this.GetRequestValBoolen("IsEdit") == false)
                            return "url@FrmVSTO.htm?1=2" + paras;
                        else
                            return "url@FrmVSTO.htm?1=2" + paras;
                    }

                    if (IsMobile == true)
                        return "url@../FrmView.htm?1=2" + paras;

                    if (md.HisFrmType == FrmType.ChapterFrm)
                    {
                        if (this.GetRequestValBoolen("Readonly") == true)
                            return "url@ChapterFrmView.htm?1=2" + paras;
                        else
                            return "url@ChapterFrm.htm?1=2" + paras;
                    }

                    if (this.GetRequestValBoolen("Readonly") == true)
                        return "url@FrmGener.htm?1=2" + paras;
                    else
                        return "url@FrmGener.htm?1=2" + paras;
                }
            }
            #endregion 非流程的独立运行的表单.

            #region 非流程的独立运行的表单.
            if (md.HisFrmType == FrmType.ChapterFrm)
            {
                if (paras.Contains("FrmID=") == false)
                    paras = paras.Replace("FK_MapData=", "FrmID=");

                //  || this.GetRequestValBoolen("IsEdit") == false @yln 这个值判断吗？
                if (IsMobile == true)
                    return "url@../FrmView.htm?1=2" + paras;
                if ((fn != null && fn.FrmSln == FrmSln.Readonly) || this.GetRequestValBoolen("Readonly") == true)
                    return "url@ChapterFrmView.htm?1=2" + paras;
                else
                {
                    return "url@ChapterFrm.htm?1=2" + paras;
                }
            }

            if (md.HisFrmType == FrmType.FoolForm)
            {
                if (IsMobile == true)
                    return "url@../FrmView.htm?1=2" + paras;
                if ((fn != null && fn.FrmSln == FrmSln.Readonly) || this.GetRequestValBoolen("Readonly") == true)
                    return "url@FrmGener.htm?1=2" + paras;
                else
                    return "url@FrmGener.htm?1=2" + paras;
            }


            if (md.HisFrmType == FrmType.WordFrm)
            {
                if ((fn != null && fn.FrmSln == FrmSln.Readonly) || this.GetRequestValBoolen("Readonly") == true)
                    return "url@FrmWord.htm?1=2" + paras;
                else
                    return "url@FrmWord.htm?1=2" + paras;
            }

            if (md.HisFrmType == FrmType.VSTOForExcel || md.HisFrmType == FrmType.ExcelFrm)
            {
                if ((fn != null && fn.FrmSln == FrmSln.Readonly) || this.GetRequestValBoolen("Readonly") == true)
                    return "url@FrmVSTO.htm?1=2" + paras;
                else
                    return "url@FrmVSTO.htm?1=2" + paras;
            }

            if (IsMobile == true)
                return "url@../FrmView.htm?1=2" + paras;

            if (this.GetRequestValBoolen("Readonly") == true)
                return "url@FrmGener.htm?1=2" + paras;
            else
                return "url@FrmGener.htm?1=2" + paras;

            #endregion 非流程的独立运行的表单.

        }

        /// <summary>
        /// 附件图片
        /// </summary>
        /// <returns></returns>
        public string FrmImgAthDB_Init()
        {
            string ImgAthPK = this.GetRequestVal("ImgAth");

            FrmImgAthDBs imgAthDBs = new FrmImgAthDBs();
            QueryObject obj = new QueryObject(imgAthDBs);
            obj.AddWhere(FrmImgAthDBAttr.FK_MapData, this.FrmID);
            obj.addAnd();
            obj.AddWhere(FrmImgAthDBAttr.FK_FrmImgAth, ImgAthPK);
            obj.addAnd();
            obj.AddWhere(FrmImgAthDBAttr.RefPKVal, this.RefPKVal);
            obj.DoQuery();

            //return BP.Tools.Entitis2Json.ConvertEntities2ListJson(imgAthDBs);
            DataTable dt = imgAthDBs.ToDataTableField();
            dt.TableName = "FrmImgAthDB";
            return BP.Tools.Json.ToJson(dt);
        }
        /// <summary>
        /// 上传编辑图片
        /// </summary>
        /// <returns></returns>
        public string FrmImgAthDB_Upload()
        {
            string CtrlID = this.GetRequestVal("CtrlID");
            int zoomW = this.GetRequestValInt("zoomW");
            int zoomH = this.GetRequestValInt("zoomH");

            //HttpFileCollection files = this.context.Request.Files;
            if (HttpContextHelper.RequestFilesCount > 0)
            {
                string myName = "";
                string fk_mapData = this.FrmID;
                if (fk_mapData.Contains("ND") == true)
                    myName = CtrlID + "_" + this.RefPKVal;
                else
                    myName = fk_mapData + "_" + CtrlID + "_" + this.RefPKVal;

                //生成新路径，解决返回相同src后图片不切换问题
                //string newName = ImgAthPK + "_" + this.MyPK + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");
                string webPath = BP.WF.Glo.CCFlowAppPath + "DataUser/ImgAth/Data/" + myName + ".png";
                //string saveToPath = this.context.Server.MapPath(BP.WF.Glo.CCFlowAppPath + "DataUser/ImgAth/Data");
                string saveToPath = BP.Difference.SystemConfig.PathOfWebApp + (BP.WF.Glo.CCFlowAppPath + "DataUser/ImgAth/Data");
                string fileUPloadPath = BP.Difference.SystemConfig.PathOfWebApp + (BP.WF.Glo.CCFlowAppPath + "DataUser/ImgAth/Upload");
                //创建路径
                if (!Directory.Exists(saveToPath))
                    Directory.CreateDirectory(saveToPath);
                if (!Directory.Exists(fileUPloadPath))
                    Directory.CreateDirectory(fileUPloadPath);

                saveToPath = saveToPath + "/" + myName + ".png";
                fileUPloadPath = fileUPloadPath + "/" + myName + ".png";
                //files[0].SaveAs(saveToPath);
                HttpContextHelper.UploadFile(HttpContextHelper.RequestFiles(0), saveToPath);

                //源图像  
                System.Drawing.Bitmap oldBmp = new System.Drawing.Bitmap(saveToPath);

                if (zoomW == 0 && zoomH == 0)
                {
                    zoomW = oldBmp.Width;
                    zoomH = oldBmp.Height;
                }

                //新图像,并设置新图像的宽高  
                System.Drawing.Bitmap newBmp = new System.Drawing.Bitmap(zoomW, zoomH);
                System.Drawing.Graphics draw = System.Drawing.Graphics.FromImage(newBmp);//从新图像获取对应的Graphics  
                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, zoomW, zoomH);//指定绘制新图像的位置和大小  
                draw.DrawImage(oldBmp, rect);//把源图像的全部完整的内容，绘制到新图像rect这个区域内，

                draw.Dispose();
                oldBmp.Dispose();//一定要把源图Dispose调，因为保存的是相同路径，需要把之前的图顶替调，如果不释放的话会报错：（GDI+ 中发生一般性错误。）  
                newBmp.Save(saveToPath);//保存替换到同一个路径 

                //复制一份
                File.Copy(saveToPath, fileUPloadPath, true);
                //获取文件大小
                FileInfo fileInfo = new FileInfo(saveToPath);
                float fileSize = 0;
                if (fileInfo.Exists)
                    fileSize = float.Parse(fileInfo.Length.ToString());

                //更新数据表                
                FrmImgAthDB imgAthDB = new FrmImgAthDB();
                imgAthDB.setMyPK(myName);
                imgAthDB.FrmID = this.FrmID;
                imgAthDB.FK_FrmImgAth = CtrlID;
                imgAthDB.RefPKVal = this.RefPKVal;
                imgAthDB.FileFullName = webPath;
                imgAthDB.FileName = fileInfo.Name;
                imgAthDB.FileExts = "png";
                imgAthDB.FileSize = fileSize;
                imgAthDB.RDT = DateTime.Now.ToString("yyyy-MM-dd mm:HH");
                imgAthDB.Rec = BP.Web.WebUser.No;
                imgAthDB.RecName = BP.Web.WebUser.Name;
                imgAthDB.Save();
                return "{\"SourceImage\":\"" + webPath + "\"}";
            }
            return "{\"err\":\"没有选择文件\"}";
        }
        public string ImgUpload_Del()
        {
            //执行删除.
            string delPK = this.GetRequestVal("DelPKVal");

            FrmImgAthDB delDB = new FrmImgAthDB();
            delDB.setMyPK(delPK == null ? this.MyPK : delPK);
            delDB.RetrieveFromDBSources();
            delDB.Delete(); //删除上传的文件.

            string saveToPath = BP.Difference.SystemConfig.PathOfWebApp + (BP.WF.Glo.CCFlowAppPath + "DataUser/ImgAth/Data");

            FileInfo fileInfo = new FileInfo(saveToPath + "/" + delDB.FileName);
            fileInfo.Delete();
            return "删除成功.";
        }
        /// <summary>
        /// 剪切图片
        /// </summary>
        /// <returns></returns>
        public string FrmImgAthDB_Cut()
        {
            string CtrlID = this.GetRequestVal("CtrlID");

            int zoomW = this.GetRequestValInt("zoomW");
            int zoomH = this.GetRequestValInt("zoomH");
            int x = this.GetRequestValInt("cX");
            int y = this.GetRequestValInt("cY");
            int w = this.GetRequestValInt("cW");
            int h = this.GetRequestValInt("cH");

            string newName = "";
            string fk_mapData = this.FrmID;
            string fileFullName = "";
            if (fk_mapData.Contains("ND") == true)
                newName = CtrlID + "_" + this.RefPKVal;
            else
                newName = fk_mapData + "_" + CtrlID + "_" + this.RefPKVal;
            //string newName = ImgAthPK + "_" + this.MyPK + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            string webPath = BP.Difference.SystemConfig.PathOfWebApp + BP.WF.Glo.CCFlowAppPath + "DataUser/ImgAth/Data/" + newName + ".png";
            string savePath = BP.Difference.SystemConfig.PathOfWebApp + BP.Difference.SystemConfig.CCFlowAppPath + "DataUser/ImgAth/Data/" + newName + ".png";
            //获取上传的大图片
            //string strImgPath = this.context.Server.MapPath(BP.Difference.SystemConfig.CCFlowWebPath + "DataUser/ImgAth/Upload/" + newName + ".png");
            string strImgPath = BP.Difference.SystemConfig.PathOfWebApp + BP.Difference.SystemConfig.CCFlowWebPath + "DataUser/ImgAth/Upload/" + newName + ".png";
            if (File.Exists(strImgPath) == true)
            {
                //剪切图
                bool bSuc = Crop(strImgPath, savePath, w, h, x, y);
                //imgAthDB.FileFullName = webPath;
                //imgAthDB.Update();
                return webPath;
            }
            return webPath;
        }

        /// <summary>
        /// 剪裁图像
        /// </summary>
        /// <param name="Img"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <returns></returns>
        private bool Crop(string Img, string savePath, int Width, int Height, int X, int Y)
        {
            try
            {
                using (var OriginalImage = new System.Drawing.Bitmap(Img))
                {
                    using (var bmp = new System.Drawing.Bitmap(Width, Height, OriginalImage.PixelFormat))
                    {
                        bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
                        using (System.Drawing.Graphics Graphic = System.Drawing.Graphics.FromImage(bmp))
                        {
                            Graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                            Graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            Graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                            Graphic.DrawImage(OriginalImage, new System.Drawing.Rectangle(0, 0, Width, Height), X, Y, Width, Height, System.Drawing.GraphicsUnit.Pixel);
                            //var ms = new MemoryStream();
                            bmp.Save(savePath);
                            //return ms.GetBuffer();
                            return true;
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                throw (Ex);
            }
            return false;
        }
        #endregion frm.htm 主表.

        #region DtlFrm
        public string DtlFrm_Init()
        {
            Int64 pk = this.RefOID;
            if (pk == 0)
                pk = this.OID;
            if (pk == 0)
                pk = this.WorkID;

            if (pk != 0)
                return FrmGener_Init();

            MapDtl dtl = new MapDtl(this.EnsName);

            GEEntityOID en = new GEEntityOID(this.EnsName);
            if (BP.Difference.SystemConfig.isBSsystem == true)
            {
                // 处理传递过来的参数。
                foreach (string k in HttpContextHelper.RequestParamKeys)
                {
                    en.SetValByKey(k, HttpContextHelper.RequestParams(k));
                }
            }

            //设置主键.
            en.SetValByKey("OID", DBAccess.GenerOID(this.EnsName));
            #region 处理权限方案。
            if (this.NodeID != 0 && this.NodeID != 999999)
            {
                Node nd = new Node(this.NodeID);
                if (nd.HisFormType == NodeFormType.SheetTree || nd.HisFormType == NodeFormType.RefOneFrmTree)
                {
                    FrmNode fn = new FrmNode(nd.NodeID, this.FrmID);
                    if (fn.FrmSln == FrmSln.Self)
                    {
                        string no = this.EnsName + "_" + nd.NodeID;
                        MapDtl mdtlSln = new MapDtl();
                        mdtlSln.No = no;
                        int result = mdtlSln.RetrieveFromDBSources();
                        if (result != 0)
                            dtl = mdtlSln;
                    }
                }
            }
            #endregion 处理权限方案。
            //给从表赋值.
            switch (dtl.DtlOpenType)
            {
                case DtlOpenType.ForEmp:  // 按人员来控制.

                    en.SetValByKey("RefPK", this.RefPKVal);
                    en.SetValByKey("FID", this.RefPKVal);
                    break;
                case DtlOpenType.ForWorkID: // 按工作ID来控制
                    en.SetValByKey("RefPK", this.RefPKVal);
                    en.SetValByKey("FID", this.RefPKVal);
                    break;
                case DtlOpenType.ForFID: // 按流程ID来控制.
                    en.SetValByKey("RefPK", this.RefPKVal);
                    en.SetValByKey("FID", this.FID);
                    break;
            }
            en.SetValByKey("RefPK", this.RefPKVal);

            en.Insert();
            string paras = "";
            string systemPara = "DoType,DoType1,DoMethod,HttpHandlerName,OID,FID,WorkID,PWorkID,RefPKVal,FK_Flow,FK_Node,IsReadonly,EnsName,FK_MapData,";
            foreach (string str in HttpContextHelper.RequestParamKeys)
            {
                if (DataType.IsNullOrEmpty(str) == true || str.Equals("T") == true || str.Equals("t") == true)
                    continue;
                if (str.Equals("IsNew") == true || str.Equals("FrmType") == true || systemPara.Contains(str + ",") == true)
                    continue;

                if (paras.Contains(str + "=") == true)
                    continue;
                paras += "&" + str + "=" + this.GetRequestVal(str);
            }
            return "url@DtlFrm.htm?EnsName=" + this.EnsName + "&RefPKVal=" + this.RefPKVal + "&FrmType=" + (int)dtl.HisEditModel + "&OID=" + en.PKVal + "&IsNew=1" + paras;
        }

        public string DtlFrm_Delete()
        {
            try
            {
                GEEntityOID en = new GEEntityOID(this.EnsName);
                en.SetValByKey("OID", this.OID);
                en.Delete();

                //如果可以上传附件这删除相应的附件信息
                FrmAttachmentDBs dbs = new FrmAttachmentDBs();
                dbs.Delete(FrmAttachmentDBAttr.FK_MapData, this.EnsName, FrmAttachmentDBAttr.RefPKVal, this.RefOID, FrmAttachmentDBAttr.NodeID, this.NodeID);


                return "删除成功.";
            }
            catch (Exception ex)
            {
                return "err@删除错误:" + ex.Message;
            }
        }

        #endregion DtlFrm

        #region frmFree
        /// <summary>
        /// 实体类的初始化
        /// </summary>
        /// <returns></returns>
        public string FrmGener_Init_ForBPClass()
        {
            try
            {

                MapData md = new MapData(this.EnsName);
                DataSet ds = BP.Sys.CCFormAPI.GenerHisDataSet(md.No);

                #region 把主表数据放入.
                string atParas = "";
                Entities ens = ClassFactory.GetEns(this.EnsName);
                Entity en = ens.GetNewEntity;
                en.PKVal = this.PKVal;

                if (en.RetrieveFromDBSources() == 0)
                    en.Insert();

                //把参数放入到 En 的 Row 里面。
                if (DataType.IsNullOrEmpty(atParas) == false)
                {
                    AtPara ap = new AtPara(atParas);
                    foreach (string key in ap.HisHT.Keys)
                    {
                        if (en.Row.ContainsKey(key) == true) //有就该变.
                            en.Row[key] = ap.GetValStrByKey(key);
                        else
                            en.Row.Add(key, ap.GetValStrByKey(key)); //增加他.
                    }
                }

                if (BP.Difference.SystemConfig.isBSsystem == true)
                {
                    // 处理传递过来的参数。
                    foreach (string k in HttpContextHelper.RequestParamKeys)
                    {
                        en.SetValByKey(k, HttpContextHelper.RequestParams(k));
                    }
                }

                // 执行表单事件. FrmLoadBefore .
                string msg = ExecEvent.DoFrm(md, EventListFrm.FrmLoadBefore, en);
                if (DataType.IsNullOrEmpty(msg) == false)
                    return "err@错误:" + msg;


                //重设默认值.
                en.ResetDefaultVal();

                //执行装载填充.
                MapExt me = new MapExt();
                if (me.Retrieve(MapExtAttr.ExtType, MapExtXmlList.PageLoadFull, MapExtAttr.FK_MapData, this.EnsName) == 1)
                {
                    //执行通用的装载方法.
                    MapAttrs attrs = new MapAttrs(this.EnsName);
                    MapDtls dtls = new MapDtls(this.EnsName);
                    en = BP.WF.CCFormAPI.DealPageLoadFull(en, me, attrs, dtls) as GEEntity;
                }

                //执行事件
                ExecEvent.DoFrm(md, EventListFrm.SaveBefore, en, null);


                //增加主表数据.
                DataTable mainTable = en.ToDataTableField(md.No);
                mainTable.TableName = "MainTable";

                ds.Tables.Add(mainTable);
                #endregion 把主表数据放入.

                #region 把外键表加入DataSet
                DataTable dtMapAttr = ds.Tables["Sys_MapAttr"];

                MapExts mes = md.MapExts;
                DataTable ddlTable = new DataTable();
                ddlTable.Columns.Add("No");
                foreach (DataRow dr in dtMapAttr.Rows)
                {
                    string lgType = dr["LGType"].ToString();
                    if (lgType.Equals("2") == false)
                        continue;

                    string UIIsEnable = dr["UIVisible"].ToString();
                    if (UIIsEnable == "0")
                        continue;

                    //string lgType = dr[MapAttrAttr.LGType].ToString();
                    //if (lgType == "0")
                    //    continue

                    string uiBindKey = dr["UIBindKey"].ToString();
                    if (DataType.IsNullOrEmpty(uiBindKey) == true)
                    {
                        string myPK = dr["MyPK"].ToString();
                        /*如果是空的*/
                        //   throw new Exception("@属性字段数据不完整，流程:" + fl.No + fl.Name + ",节点:" + nd.NodeID + nd.Name + ",属性:" + myPK + ",的UIBindKey IsNull ");
                    }

                    // 检查是否有下拉框自动填充。
                    string keyOfEn = dr["KeyOfEn"].ToString();
                    string fk_mapData = dr["FK_MapData"].ToString();

                    #region 处理下拉框数据范围. for 小杨.
                    me = mes.GetEntityByKey(MapExtAttr.ExtType, MapExtXmlList.AutoFullDLL, MapExtAttr.AttrOfOper, keyOfEn) as MapExt;
                    if (me != null)
                    {
                        string fullSQL = me.Doc.Clone() as string;
                        fullSQL = fullSQL.Replace("~", ",");
                        fullSQL = BP.WF.Glo.DealExp(fullSQL, en, null);
                        DataTable dt = DBAccess.RunSQLReturnTable(fullSQL);
                        dt.TableName = keyOfEn; //可能存在隐患，如果多个字段，绑定同一个表，就存在这样的问题.
                        ds.Tables.Add(dt);
                        continue;
                    }
                    #endregion 处理下拉框数据范围.

                    // 判断是否存在.
                    if (ds.Tables.Contains(uiBindKey) == true)
                        continue;

                    DataTable dataTable = BP.Pub.PubClass.GetDataTableByUIBineKey(uiBindKey);
                    if (dataTable != null)
                        ds.Tables.Add(dataTable);
                    else
                    {
                        DataRow ddldr = ddlTable.NewRow();
                        ddldr["No"] = uiBindKey;
                        ddlTable.Rows.Add(ddldr);
                    }
                }
                ddlTable.TableName = "UIBindKey";
                ds.Tables.Add(ddlTable);
                #endregion End把外键表加入DataSet

                return BP.Tools.Json.DataSetToJson(ds, false);
            }
            catch (Exception ex)
            {
                GEEntity myen = new GEEntity(this.EnsName);
                myen.CheckPhysicsTable();

                BP.Sys.CCFormAPI.RepareCCForm(this.EnsName);
                return "err@装载表单期间出现如下错误 FrmGener_Init_ForBPClass ,ccform有自动诊断修复功能请在刷新一次，如果仍然存在请联系管理员. @" + ex.Message;
            }
        }

        public string FrmGener_Init_ForDBList()
        {
            try
            {
                CCBill.DBList dblist = new CCBill.DBList(this.EnsName);
                MapData md = new MapData(this.EnsName);
                DataSet ds = BP.Sys.CCFormAPI.GenerHisDataSet(dblist.No);

                #region 把主表数据放入.
                string atParas = "";
                GEEntity en = new GEEntity(this.EnsName);



                string pk = this.GetRequestVal("OID");
                if (DataType.IsNullOrEmpty(pk) == true)
                    pk = this.GetRequestVal("WorkID");
                string expEn = dblist.ExpEn;
                expEn = expEn.Replace("@Key", pk);
                if (expEn.Contains("@") == true)
                {
                    expEn = expEn.Replace("@WebUser.No", WebUser.No);
                    expEn = expEn.Replace("@WebUser.Name", WebUser.Name);
                    expEn = expEn.Replace("@WebUser.FK_DeptName", WebUser.DeptName);
                    expEn = expEn.Replace("@WebUser.FK_Dept", WebUser.DeptNo);
                    expEn = expEn.Replace("@WebUser.DeptNo", WebUser.DeptNo);

                }
                if (dblist.DBType == 0)
                {
                    expEn = expEn.Replace("~", "'");
                    if (DataType.IsNullOrEmpty(dblist.DBSrc) == true)
                        dblist.DBSrc = "local";

                    SFDBSrc sf = new SFDBSrc(dblist.DBSrc);
                    DataTable dt = sf.RunSQLReturnTable(expEn);
                    if (dt.Rows.Count == 1)
                    {
                        //把DataTable中的内容转换成Entity的值
                        foreach (MapAttr attr in md.MapAttrs)
                        {
                            if (dt.Columns.Contains(attr.KeyOfEn) == true)
                                en.Row.SetValByKey(attr.KeyOfEn, dt.Rows[0][attr.KeyOfEn]);
                        }
                    }
                }

                if (dblist.DBType == 1)
                {
                    if (expEn.Contains("http") == false)
                    {
                        /*如果没有绝对路径 */
                        if (BP.Difference.SystemConfig.isBSsystem)
                        {
                            /*在cs模式下自动获取*/
                            string host = HttpContextHelper.RequestUrlHost;//BP.Sys.Base.Glo.Request.Url.Host;
                            if (expEn.Contains("@AppPath"))
                                expEn = expEn.Replace("@AppPath", "http://" + host + HttpContextHelper.RequestApplicationPath);//BP.Sys.Base.Glo.Request.ApplicationPath
                            else
                                expEn = "http://" + HttpContextHelper.RequestUrlAuthority + expEn;
                        }

                        if (BP.Difference.SystemConfig.isBSsystem == false)
                        {
                            /*在cs模式下它的baseurl 从web.config中获取.*/
                            string cfgBaseUrl = BP.Difference.SystemConfig.AppSettings["HostURL"];
                            if (DataType.IsNullOrEmpty(cfgBaseUrl))
                            {
                                string err = "调用url失败:没有在web.config中配置BaseUrl,导致url事件不能被执行.";
                                BP.DA.Log.DebugWriteError(err);
                                throw new Exception(err);
                            }
                            expEn = cfgBaseUrl + expEn;
                        }
                    }
                    System.Text.Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
                    string json = DataType.ReadURLContext(expEn, 8000, encode);
                    if (DataType.IsNullOrEmpty(json) == false)
                    {
                        DataTable dt = BP.Tools.Json.ToDataTable(json);
                        if (dt.Rows.Count == 1)
                        {
                            //把DataTable中的内容转换成Entity的值
                            foreach (MapAttr attr in md.MapAttrs)
                            {
                                if (dt.Columns.Contains(attr.KeyOfEn) == true)
                                    en.Row.SetValByKey(attr.KeyOfEn, dt.Rows[0][attr.KeyOfEn]);
                            }
                        }
                    }

                }


                //把参数放入到 En 的 Row 里面。
                if (DataType.IsNullOrEmpty(atParas) == false)
                {
                    AtPara ap = new AtPara(atParas);
                    foreach (string key in ap.HisHT.Keys)
                    {
                        if (en.Row.ContainsKey(key) == true) //有就该变.
                            en.Row[key] = ap.GetValStrByKey(key);
                        else
                            en.Row.Add(key, ap.GetValStrByKey(key)); //增加他.
                    }
                }

                if (BP.Difference.SystemConfig.isBSsystem == true)
                {
                    // 处理传递过来的参数。
                    foreach (string k in HttpContextHelper.RequestParamKeys)
                    {
                        en.SetValByKey(k, HttpContextHelper.RequestParams(k));
                    }
                }

                // 执行表单事件. FrmLoadBefore .
                en.SetPara("FrmType", "DBList");
                string msg = ExecEvent.DoFrm(md, EventListFrm.FrmLoadBefore, en);
                if (DataType.IsNullOrEmpty(msg) == false)
                    return "err@错误:" + msg;

                //增加主表数据.
                DataTable mainTable = en.ToDataTableField(md.No);
                mainTable.TableName = "MainTable";

                ds.Tables.Add(mainTable);
                #endregion 把主表数据放入.

                #region 把外键表加入DataSet
                DataTable dtMapAttr = ds.Tables["Sys_MapAttr"];

                MapExts mes = md.MapExts;
                DataTable ddlTable = new DataTable();
                ddlTable.Columns.Add("No");
                foreach (DataRow dr in dtMapAttr.Rows)
                {
                    string lgType = dr["LGType"].ToString();
                    if (lgType.Equals("2") == false)
                        continue;

                    string UIIsEnable = dr["UIVisible"].ToString();
                    if (UIIsEnable == "0")
                        continue;


                    string uiBindKey = dr["UIBindKey"].ToString();
                    if (DataType.IsNullOrEmpty(uiBindKey) == true)
                    {
                        string myPK = dr["MyPK"].ToString();

                    }

                    // 检查是否有下拉框自动填充。
                    string keyOfEn = dr["KeyOfEn"].ToString();
                    string fk_mapData = dr["FK_MapData"].ToString();

                    #region 处理下拉框数据范围. for 小杨.
                    MapExt me = mes.GetEntityByKey(MapExtAttr.ExtType, MapExtXmlList.AutoFullDLL, MapExtAttr.AttrOfOper, keyOfEn) as MapExt;
                    if (me != null)
                    {
                        string fullSQL = me.Doc.Clone() as string;
                        fullSQL = fullSQL.Replace("~", ",");
                        fullSQL = BP.WF.Glo.DealExp(fullSQL, en, null);
                        DataTable dt = DBAccess.RunSQLReturnTable(fullSQL);
                        dt.TableName = keyOfEn; //可能存在隐患，如果多个字段，绑定同一个表，就存在这样的问题.
                        ds.Tables.Add(dt);
                        continue;
                    }
                    #endregion 处理下拉框数据范围.

                    // 判断是否存在.
                    if (ds.Tables.Contains(uiBindKey) == true)
                        continue;

                    DataTable dataTable = BP.Pub.PubClass.GetDataTableByUIBineKey(uiBindKey);
                    if (dataTable != null)
                        ds.Tables.Add(dataTable);
                    else
                    {
                        DataRow ddldr = ddlTable.NewRow();
                        ddldr["No"] = uiBindKey;
                        ddlTable.Rows.Add(ddldr);
                    }
                }
                ddlTable.TableName = "UIBindKey";
                ds.Tables.Add(ddlTable);
                #endregion End把外键表加入DataSet

                return BP.Tools.Json.DataSetToJson(ds, false);
            }
            catch (Exception ex)
            {
                GEEntity myen = new GEEntity(this.EnsName);
                myen.CheckPhysicsTable();

                BP.Sys.CCFormAPI.RepareCCForm(this.EnsName);
                return "err@装载表单期间出现如下错误 FrmGener_Init_ForDBList ,ccform有自动诊断修复功能请在刷新一次，如果仍然存在请联系管理员. @" + ex.Message;
            }
        }
        /// <summary>
        /// 表单打开
        /// </summary>
        /// <returns></returns>
        public string WpsFrm_Init()
        {
            Int64 workID = this.WorkID;

            GenerWorkFlow gwf = new GenerWorkFlow(this.WorkID);
            if (gwf.PWorkID != 0)
                workID = gwf.PWorkID;
            ///获得表单wps.
            MapFrmWps md = new MapFrmWps(this.FrmID);

            //首先从数据表里获取二进制表单实例.
            string file = BP.Difference.SystemConfig.PathOfTemp + "\\" + workID + "." + this.FrmID + ".docx";

            string templateFilePath = BP.Difference.SystemConfig.PathOfCyclostyleFile + md.No + "." + md.MyFileExt;

            //生成文件.
            byte[] val = BP.DA.DBAccess.GetFileFromDB(file, md.PTable, "OID", workID.ToString(), "DBFile");
            if (val == null)
                System.IO.File.Copy(templateFilePath, file, true);

            return "/DataUser/Temp/" + workID + "." + this.FrmID + ".docx";
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <returns></returns>
        public string WpsFrm_SaveFile()
        {
            // string fileName = "c:\\xxxx\temp.px";
            Int64 workID = this.WorkID;

            GenerWorkFlow gwf = new GenerWorkFlow(this.WorkID);
            if (gwf.PWorkID != 0)
                workID = gwf.PWorkID;

            string fileName = BP.Difference.SystemConfig.PathOfTemp + "\\" + workID + "." + this.FrmID + ".docx";


            MapData md = new MapData(this.FrmID);

            ///     string strs = this.RequestParas;

            IFormFile file = HttpContextHelper.RequestFiles()[0];  //context.Request.Files;
            //file.SaveAs(fileName);

            //保存文件.
            DBAccess.SaveFileToDB(fileName, md.PTable, "OID", workID.ToString(), "DBFile");


            return "上传成功.";
        }
        private Entity InitTest(Entity en)
        {
            foreach (Attr attr in en.EnMap.Attrs)
            {
                if (attr.MyDataType == DataType.AppString && attr.UIBindKey.Length == 0)
                {
                    en.SetValByKey(attr.Key, "测试");
                    continue;
                }

                if (attr.MyDataType == DataType.AppInt && attr.UIContralType != UIContralType.TB)
                {
                    SysEnum se = new SysEnum();
                    se.EnumKey = attr.UIBindKey;
                    se.IntKey = 1;
                    se.ResetPK();
                    se.RetrieveFromDBSources();

                    en.SetValByKey(attr.Key, 1);
                    en.SetValByKey(attr.Key + "Text", se.Lab);
                    continue;
                }

                if (attr.MyDataType == DataType.AppMoney)
                {
                    en.SetValByKey(attr.Key, "123.12");
                    continue;
                }
                if (attr.MyDataType == DataType.AppBoolean)
                {
                    en.SetValByKey(attr.Key, "1");
                    continue;
                }
                if (attr.MyDataType == DataType.AppDate)
                {
                    en.SetValByKey(attr.Key, DataType.CurrentDate);
                    continue;
                }
                if (attr.MyDataType == DataType.AppDateTime)
                {
                    en.SetValByKey(attr.Key, DataType.CurrentDateTimess);
                    continue;
                }

                if (attr.ItIsNum == true)
                {
                    en.SetValByKey(attr.Key, "123456");
                    continue;
                }
            }
            return en;
        }
        public string FrmGener_Init_ForEntityNoName()
        {
            MapData md = new MapData(this.EnsName);
            //是否启用装载填充？
            bool isLoadData = true;
            try
            {
                //主表实体.
                GEEntityNoName en = new GEEntityNoName(this.EnsName);
                string pk = this.RefNo;
                en.No = pk;
                if (DataType.IsNullOrEmpty(pk) == true)
                {
                    en.ResetDefaultVal();
                    en = this.InitTest(en) as GEEntityNoName; 
                }
                else
                {
                    if (en.RetrieveFromDBSources() == 0)
                        en.Insert();
                }

                DataSet ds = BP.Sys.CCFormAPI.GenerHisDataSet(this.EnsName);

                #region 附加参数数据.
                if (BP.Difference.SystemConfig.isBSsystem == true)
                {
                    // 处理传递过来的参数。
                    foreach (string k in HttpContextHelper.RequestParamKeys)
                    {
                        en.SetValByKey(k, HttpContextHelper.RequestParams(k));
                    }
                }

                // 执行表单事件. FrmLoadBefore .
                string msg = ExecEvent.DoFrm(md, EventListFrm.FrmLoadBefore, en);
                if (DataType.IsNullOrEmpty(msg) == false)
                    return "err@错误:" + msg;

                //重设默认值.
                if (this.GetRequestValBoolen("IsReadonly") == false)
                    en.ResetDefaultVal();

                #endregion 附加参数数据.

                #region 执行装载填充.与相关的事件.
                MapExts mes = md.MapExts;

                MapExt me = mes.GetEntityByKey("ExtType", MapExtXmlList.PageLoadFull) as MapExt;
                if (me == null)
                    me = mes.GetEntityByKey("ExtModel", MapExtXmlList.PageLoadFullMainTable) as MapExt;
                if (isLoadData == true && md.ItIsPageLoadFull && me != null && GetRequestValInt("IsTest") != 1)
                {
                    //执行通用的装载方法.
                    MapAttrs attrs = new MapAttrs(this.EnsName);
                    MapDtls dtls = new MapDtls(this.EnsName);
                    //判断是否自定义权限.
                    bool IsSelf = false;
                    //单据或者是单据实体表单

                    en = BP.WF.CCFormAPI.DealPageLoadFull(en, me, attrs, dtls, IsSelf, 0, this.WorkID) as GEEntityNoName;
                }
                #endregion 执行装载填充.与相关的事件.

                #region 把外键表加入 DataSet.
                DataTable dtMapAttr = ds.Tables["Sys_MapAttr"];

                DataTable ddlTable = new DataTable();
                ddlTable.Columns.Add("No");
                foreach (DataRow dr in dtMapAttr.Rows)
                {
                    string lgType = dr["LGType"].ToString();
                    string uiBindKey = dr["UIBindKey"].ToString();

                    string uiVisible = dr["UIVisible"].ToString();
                    if (uiVisible.Equals("0") == true)
                        continue;

                    if (DataType.IsNullOrEmpty(uiBindKey) == true)
                        continue; //为空就continue.

                    if (lgType.Equals("1") == true)
                        continue; //枚举值就continue;

                    string uiIsEnable = dr["UIIsEnable"].ToString();
                    if (uiIsEnable.Equals("0") == true && lgType.Equals("1") == true)
                        continue; //如果是外键，并且是不可以编辑的状态.

                    if (uiIsEnable.Equals("0") == true && lgType.Equals("0") == true)
                        continue; //如果是外部数据源，并且是不可以编辑的状态.

                    // 检查是否有下拉框自动填充。
                    string keyOfEn = dr["KeyOfEn"].ToString();
                    string fk_mapData = dr["FK_MapData"].ToString();

                    #region 处理下拉框数据范围. for 小杨.
                    me = mes.GetEntityByKey(MapExtAttr.ExtType, MapExtXmlList.AutoFullDLL, MapExtAttr.AttrOfOper, keyOfEn) as MapExt;
                    if (me != null)
                    {
                        string fullSQL = me.Doc.Clone() as string;
                        fullSQL = fullSQL.Replace("~", ",");
                        fullSQL = BP.WF.Glo.DealExp(fullSQL, en, null);
                        DataTable dt = DBAccess.RunSQLReturnTable(fullSQL);
                        if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel == FieldCaseModel.UpperCase)
                        {
                            if (dt.Columns.Contains("NO") == true)
                                dt.Columns["NO"].ColumnName = "No";
                            if (dt.Columns.Contains("NAME") == true)
                                dt.Columns["NAME"].ColumnName = "Name";
                            if (dt.Columns.Contains("PARENTNO") == true)
                                dt.Columns["PARENTNO"].ColumnName = "ParentNo";
                        }

                        if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel == FieldCaseModel.Lowercase)
                        {
                            if (dt.Columns.Contains("no") == true)
                                dt.Columns["no"].ColumnName = "No";
                            if (dt.Columns.Contains("name") == true)
                                dt.Columns["name"].ColumnName = "Name";
                            if (dt.Columns.Contains("parentno") == true)
                                dt.Columns["parentno"].ColumnName = "ParentNo";
                        }

                        dt.TableName = keyOfEn; //可能存在隐患，如果多个字段，绑定同一个表，就存在这样的问题.
                        ds.Tables.Add(dt);
                        continue;
                    }
                    #endregion 处理下拉框数据范围.

                    // 判断是否存在.
                    if (ds.Tables.Contains(uiBindKey) == true)
                        continue;

                    DataTable dataTable = BP.Pub.PubClass.GetDataTableByUIBineKey(uiBindKey);

                    if (dataTable != null)
                        ds.Tables.Add(dataTable);
                    else
                    {
                        DataRow ddldr = ddlTable.NewRow();
                        ddldr["No"] = uiBindKey;
                        ddlTable.Rows.Add(ddldr);
                    }
                }
                ddlTable.TableName = "UIBindKey";
                ds.Tables.Add(ddlTable);
                #endregion End把外键表加入DataSet


                #region 加入主表的数据.
                //增加主表数据.
                DataTable mainTable = en.ToDataTableField("MainTable");
                ds.Tables.Add(mainTable);
                #endregion 加入主表的数据.
                // 执行表单事件. FrmLoadAfter .
                msg = ExecEvent.DoFrm(md, EventListFrm.FrmLoadAfter, en);
                if (DataType.IsNullOrEmpty(msg) == false)
                    return "err@错误:" + msg;
                string json = BP.Tools.Json.DataSetToJson(ds, false);

                //DataType.WriteFile("c:/aaa.txt", json);
                return json;
            }
            catch (Exception ex)
            {
                GEEntityNoName myen = new GEEntityNoName(this.EnsName);
                myen.CheckPhysicsTable();

                BP.Sys.CCFormAPI.RepareCCForm(this.EnsName);
                return "err@装载表单期间出现如下错误FrmGener_Init,ccform有自动诊断修复功能请在刷新一次，如果仍然存在请联系管理员. @" + ex.Message;
            }

        }
        /// <summary>
        /// 执行数据初始化
        /// </summary>
        /// <returns></returns>
        public string FrmGener_Init()
        {
            if (this.FrmID != null && this.FrmID.ToUpper().Contains("BP.") == true)
                return FrmGener_Init_ForBPClass();
            MapData md = new MapData(this.EnsName);
            if (md.EntityType == EntityType.DBList)
                return FrmGener_Init_ForDBList();

            if (md.EntityType == EntityType.FrmEntityNoName)
                return FrmGener_Init_ForEntityNoName();

            #region 定义流程信息的所用的 配置entity.
            //节点与表单的权限控制.
            FrmNode fn = null;

            //是否启用装载填充？
            bool isLoadData = true;
            //定义节点变量. 
            Node nd = null;
            if (this.NodeID != 0 && this.NodeID != 999999)
            {
                nd = new Node(this.NodeID);
                nd.WorkID = this.WorkID; //为获取表单ID ( NodeFrmID )提供参数.
                //if (nd.HisFormType== NodeFormType.FoolTruck)

                fn = new FrmNode(this.NodeID, this.FrmID);
                isLoadData = fn.ItIsEnableLoadData;
            }
            #endregion 定义流程信息的所用的 配置entity.

            try
            {
                #region 特殊判断 适应累加表单.
                string fromWhere = this.GetRequestVal("FromWorkOpt");
                if (fromWhere != null && fromWhere.Equals("1") && this.NodeID != 0 && this.NodeID != 999999)
                {
                    //如果是累加表单.
                    if (nd.HisFormType == NodeFormType.FoolTruck)
                    {
                        DataSet myds = BP.WF.CCFlowAPI.GenerWorkNode(this.FlowNo,
                            nd, this.WorkID,
                  this.FID, BP.Web.WebUser.No, this.WorkID, this.GetRequestVal("FromWorkOpt"));

                        return BP.Tools.Json.ToJson(myds);
                    }
                }
                #endregion 特殊判断.适应累加表单.

                //主表实体.
                GEEntityOID en = new GEEntityOID(this.EnsName);

                Int64 pk = this.RefOID;
                if (pk == 0)
                    pk = this.OID;
                if (pk == 0)
                    pk = this.WorkID;

                #region 根据who is pk 获取数据.
                en.OID = pk;
                if (en.OID == 0)
                {
                    en.ResetDefaultVal();

                    en = this.InitTest(en) as GEEntityOID;
                }
                else
                {
                    if (en.RetrieveFromDBSources() == 0)
                        en.Insert();
                }
                #endregion 根据who is pk 获取数据.
                string frmID = md.No;
                //根据表单存储的数据获取获取使用表单的版本号
                int frmVer = 0;
                if (en.EnMap.Attrs.Contains("AtPara") == true)
                {
                    frmVer = en.GetParaInt("FrmVer");
                    if (frmVer != 0 && frmVer != md.Ver2022)
                    {
                        frmID = md.No + "." + frmVer;
                        if (nd.FormType != NodeFormType.FoolTruck)
                        {
                            en = new GEEntityOID(frmID);
                            en.OID = pk;
                            en.RetrieveFromDBSources();
                        }
                    }
                }

                DataSet ds = BP.Sys.CCFormAPI.GenerHisDataSet(frmID);
                //现在版本不是主版本的情况
                if (frmID.Equals(this.FrmID) == false)
                {
                    DataTable mddt = ds.Tables["Sys_MapData"];
                    mddt.Rows[0]["AtPara"] = mddt.Rows[0]["AtPara"] + "@MainFrmID=" + this.FrmID;
                    //如果是傻瓜表单
                    if (md.HisFrmType == FrmType.FoolForm)
                    {
                        DataTable athdt = ds.Tables["Sys_FrmAttachment"];
                        if (frmVer != 0 && athdt.Rows.Count != 0)
                        {
                            DataTable gfdt = ds.Tables["Sys_GroupField"];
                            foreach (DataRow dr in athdt.Rows)
                            {
                                DataRow[] gfr = gfdt.Select("CtrlID='" + dr["MyPK"] + "'");
                                if (gfr.Length != 0)
                                    gfr[0]["CtrlID"] = md.No + "_" + dr["NoOfObj"];
                                dr["MyPK"] = md.No + "_" + dr["NoOfObj"];
                            }
                        }
                    }
                }

                //如果有框架
                if (ds.Tables["Sys_MapFrame"].Rows.Count > 0)
                {
                    //把流程信息表发送过去.
                    GenerWorkFlow gwf = new GenerWorkFlow();
                    gwf.WorkID = pk;
                    gwf.RetrieveFromDBSources();
                    ds.Tables.Add(gwf.ToDataTableField("WF_GenerWorkFlow"));
                }

                #region 附加参数数据.
                if (BP.Difference.SystemConfig.isBSsystem == true)
                {
                    // 处理传递过来的参数。
                    foreach (string k in HttpContextHelper.RequestParamKeys)
                    {
                        if (DataType.IsNullOrEmpty(k) || k.Equals("AtPara"))
                            continue;
                        en.SetValByKey(k, HttpContextHelper.RequestParams(k));
                    }
                }

                // 执行表单事件. FrmLoadBefore .
                string msg = ExecEvent.DoFrm(md, EventListFrm.FrmLoadBefore, en);
                if (DataType.IsNullOrEmpty(msg) == false)
                    return "err@错误:" + msg;

                //重设默认值.
                if (this.GetRequestValBoolen("IsReadonly") == false)
                    en.ResetDefaultVal();

                #endregion 附加参数数据.

                #region 执行装载填充.与相关的事件.
                MapExts mes = md.MapExts;
                string pageType = this.GetRequestVal("PageType");
                MapExt me = null;
                if (DataType.IsNullOrEmpty(pageType) == true)
                {
                    me = mes.GetEntityByKey("ExtType", MapExtXmlList.PageLoadFull) as MapExt;
                    if (isLoadData == true && md.ItIsPageLoadFull && me != null && GetRequestValInt("IsTest") != 1)
                    {
                        //执行通用的装载方法.
                        MapAttrs attrs = new MapAttrs(this.EnsName);
                        MapDtls dtls = new MapDtls(this.EnsName);
                        //判断是否自定义权限.
                        bool IsSelf = false;
                        //单据或者是单据实体表单
                        if (nd == null)
                        {
                            en = BP.WF.CCFormAPI.DealPageLoadFull(en, me, attrs, dtls, IsSelf, 0, this.WorkID) as GEEntityOID;
                        }
                        else
                        {
                            if ((nd.HisFormType == NodeFormType.SheetTree
                                || nd.HisFormType == NodeFormType.RefOneFrmTree)
                                && (fn.FrmSln == FrmSln.Self))
                                IsSelf = true;
                            en = BP.WF.CCFormAPI.DealPageLoadFull(en, me, attrs, dtls, IsSelf, nd.NodeID, this.WorkID) as GEEntityOID;
                        }
                    }
                }
                if (DataType.IsNullOrEmpty(pageType) == false && pageType.Equals("Vue3") && GetRequestValInt("IsTest") != 1)
                {
                    me = mes.GetEntityByKey("ExtModel", MapExtXmlList.PageLoadFullMainTable) as MapExt;
                    if (isLoadData == true && me != null && me.DoWay.Equals("None") == false)
                    {
                        //执行通用的装载方法.
                        MapAttrs attrs = md.MapAttrs;
                        en = BP.WF.CCFormAPI.DealPageLoadFullVue(en, mes, attrs) as GEEntityOID;
                        en.Update();
                    }
                }
                //执行事件, 不应该加.
                if (1 == 2)
                    ExecEvent.DoFrm(md, EventListFrm.SaveBefore, en, null);
                #endregion 执行装载填充.与相关的事件.

                #region 把外键表加入 DataSet.
                DataTable dtMapAttr = ds.Tables["Sys_MapAttr"];

                DataTable ddlTable = new DataTable();
                ddlTable.Columns.Add("No");
                foreach (DataRow dr in dtMapAttr.Rows)
                {
                    string lgType = dr["LGType"].ToString();
                    string uiBindKey = dr["UIBindKey"].ToString();

                    string uiVisible = dr["UIVisible"].ToString();
                    if (uiVisible.Equals("0") == true)
                        continue;

                    if (DataType.IsNullOrEmpty(uiBindKey) == true)
                        continue; //为空就continue.

                    if (lgType.Equals("1") == true)
                        continue; //枚举值就continue;

                    string uiIsEnable = dr["UIIsEnable"].ToString();
                    if (uiIsEnable.Equals("0") == true && lgType.Equals("1") == true)
                        continue; //如果是外键，并且是不可以编辑的状态.

                    if (uiIsEnable.Equals("0") == true && lgType.Equals("0") == true)
                        continue; //如果是外部数据源，并且是不可以编辑的状态.

                    // 检查是否有下拉框自动填充。
                    string keyOfEn = dr["KeyOfEn"].ToString();
                    string fk_mapData = dr["FK_MapData"].ToString();

                    #region 处理下拉框数据范围. for 小杨.
                    me = mes.GetEntityByKey(MapExtAttr.ExtType, MapExtXmlList.AutoFullDLL, MapExtAttr.AttrOfOper, keyOfEn) as MapExt;
                    if (me != null)
                    {
                        string fullSQL = me.Doc.Clone() as string;
                        fullSQL = fullSQL.Replace("~", ",");
                        fullSQL = BP.WF.Glo.DealExp(fullSQL, en, null);
                        DataTable dt = DBAccess.RunSQLReturnTable(fullSQL);
                        if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel == FieldCaseModel.UpperCase)
                        {
                            if (dt.Columns.Contains("NO") == true)
                                dt.Columns["NO"].ColumnName = "No";
                            if (dt.Columns.Contains("NAME") == true)
                                dt.Columns["NAME"].ColumnName = "Name";
                            if (dt.Columns.Contains("PARENTNO") == true)
                                dt.Columns["PARENTNO"].ColumnName = "ParentNo";
                        }

                        if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel == FieldCaseModel.Lowercase)
                        {
                            if (dt.Columns.Contains("no") == true)
                                dt.Columns["no"].ColumnName = "No";
                            if (dt.Columns.Contains("name") == true)
                                dt.Columns["name"].ColumnName = "Name";
                            if (dt.Columns.Contains("parentno") == true)
                                dt.Columns["parentno"].ColumnName = "ParentNo";
                        }

                        dt.TableName = keyOfEn; //可能存在隐患，如果多个字段，绑定同一个表，就存在这样的问题.
                        ds.Tables.Add(dt);
                        continue;
                    }
                    #endregion 处理下拉框数据范围.

                    // 判断是否存在.
                    if (ds.Tables.Contains(uiBindKey) == true)
                        continue;

                    DataTable dataTable = BP.Pub.PubClass.GetDataTableByUIBineKey(uiBindKey);

                    if (dataTable != null)
                        ds.Tables.Add(dataTable);
                    else
                    {
                        DataRow ddldr = ddlTable.NewRow();
                        ddldr["No"] = uiBindKey;
                        ddlTable.Rows.Add(ddldr);
                    }
                }
                ddlTable.TableName = "UIBindKey";
                ds.Tables.Add(ddlTable);
                #endregion End把外键表加入DataSet

                #region 加入组件的状态信息, 在解析表单的时候使用.
                if (nd != null && fn.IsEnableFWC != FrmWorkCheckSta.Disable)
                {
                    BP.WF.Template.FrmNodeComponent fnc = new FrmNodeComponent(nd.NodeID);
                    if (nd.NodeFrmID != "ND" + nd.NodeID)
                    {
                        /*说明这是引用到了其他节点的表单，就需要把一些位置元素修改掉.*/
                        int refNodeID = 0;
                        if (nd.NodeFrmID.IndexOf("ND") == -1)
                            refNodeID = nd.NodeID;
                        else
                            refNodeID = int.Parse(nd.NodeFrmID.Replace("ND", ""));

                        BP.WF.Template.FrmNodeComponent refFnc = new FrmNodeComponent(refNodeID);

                        fnc.SetValByKey(NodeWorkCheckAttr.FWC_H, refFnc.GetValFloatByKey(NodeWorkCheckAttr.FWC_H));

                        fnc.SetValByKey(FrmSubFlowAttr.SF_H, refFnc.GetValFloatByKey(FrmSubFlowAttr.SF_H));


                        fnc.SetValByKey(FrmTrackAttr.FrmTrack_H, refFnc.GetValFloatByKey(FrmTrackAttr.FrmTrack_H));

                        fnc.SetValByKey(FTCAttr.FTC_H, refFnc.GetValFloatByKey(FTCAttr.FTC_H));
                        if (md.HisFrmType == FrmType.FoolForm)
                        {
                            //判断是否是傻瓜表单，如果是，就要判断该傻瓜表单是否有审核组件groupfield ,没有的话就增加上.
                            DataTable gf = ds.Tables["Sys_GroupField"];
                            bool isHave = false;
                            foreach (DataRow dr in gf.Rows)
                            {
                                string cType = dr["CtrlType"] as string;
                                if (cType == null)
                                    continue;

                                if (cType.Equals("FWC") == true)
                                    isHave = true;
                            }

                            if (isHave == false)
                            {
                                DataRow dr = gf.NewRow();

                                nd.WorkID = this.WorkID; //为获取表单ID提供参数.
                                dr[GroupFieldAttr.OID] = 100;
                                dr[GroupFieldAttr.FrmID] = nd.NodeFrmID;
                                dr[GroupFieldAttr.CtrlType] = "FWC";
                                dr[GroupFieldAttr.CtrlID] = "FWCND" + nd.NodeID;
                                dr[GroupFieldAttr.Idx] = 100;
                                dr[GroupFieldAttr.Lab] = "审核信息";
                                gf.Rows.Add(dr);

                                ds.Tables.Remove("Sys_GroupField");
                                ds.Tables.Add(gf);

                                //执行更新,就自动生成那个丢失的字段分组.
                                refFnc.Update(); //这里生成到了NDxxx表单上去了。

                            }
                        }
                    }


                    #region 没有审核组件分组就增加上审核组件分组.
                    if (fn.IsEnableFWC != FrmWorkCheckSta.Disable)
                    {
                        //如果启用了审核组件，并且 节点表单与当前一致。
                        if (md.HisFrmType == FrmType.FoolForm || md.HisFrmType == FrmType.WPSFrm)
                        {
                            //判断是否是傻瓜表单，如果是，就要判断该傻瓜表单是否有审核组件groupfield ,没有的话就增加上.
                            DataTable gf = ds.Tables["Sys_GroupField"];
                            bool isHave = false;
                            foreach (DataRow dr in gf.Rows)
                            {
                                string cType = dr["CtrlType"] as string;
                                if (cType == null)
                                    continue;

                                if (cType.Equals("FWC") == true)
                                    isHave = true;
                            }

                            if (isHave == false)
                            {
                                DataRow dr = gf.NewRow();

                                nd.WorkID = this.WorkID; //为获取表单ID提供参数.
                                dr[GroupFieldAttr.OID] = 100;
                                dr[GroupFieldAttr.FrmID] = nd.NodeFrmID;
                                dr[GroupFieldAttr.CtrlType] = "FWC";
                                dr[GroupFieldAttr.CtrlID] = "FWCND" + nd.NodeID;
                                dr[GroupFieldAttr.Idx] = 100;
                                dr[GroupFieldAttr.Lab] = "审核信息";
                                gf.Rows.Add(dr);

                                ds.Tables.Remove("Sys_GroupField");
                                ds.Tables.Add(gf);

                                //更新,为了让其表单上自动增加审核分组.
                                BP.WF.Template.FrmNodeComponent refFnc = new FrmNodeComponent(nd.NodeID);
                                NodeWorkCheck fwc = new NodeWorkCheck(nd.NodeID);
                                if (fn.FrmSln == FrmSln.Self || fn.FrmSln == FrmSln.Default)
                                    fwc.HisFrmWorkCheckSta = FrmWorkCheckSta.Enable;
                                else
                                    fwc.HisFrmWorkCheckSta = FrmWorkCheckSta.Readonly;

                                refFnc.Update();
                            }
                        }
                    }
                    #endregion 没有审核组件分组就增加上审核组件分组.
                    ds.Tables.Add(fnc.ToDataTableField("WF_FrmNodeComponent"));
                }
                if (nd != null)
                    ds.Tables.Add(nd.ToDataTableField("WF_Node"));

                if (nd != null)
                    ds.Tables.Add(fn.ToDataTableField("WF_FrmNode"));

                #endregion 加入组件的状态信息, 在解析表单的时候使用.

                #region 处理权限方案
                if (nd != null && nd.FormType == NodeFormType.SheetTree)
                {
                    #region 只读方案.
                    if (fn.FrmSln == FrmSln.Readonly)
                    {
                        foreach (DataRow dr in dtMapAttr.Rows)
                        {
                            dr[MapAttrAttr.UIIsEnable] = 0;
                        }

                        //改变他的属性. 不知道是否应该这样写？
                        ds.Tables.Remove("Sys_MapAttr");
                        ds.Tables.Add(dtMapAttr);
                    }
                    #endregion 只读方案.

                    #region 自定义方案.
                    if (fn.FrmSln == FrmSln.Self)
                    {
                        //查询出来自定义的数据.
                        FrmFields ffs = new FrmFields();
                        ffs.Retrieve(FrmFieldAttr.FK_Node, nd.NodeID,
                            FrmFieldAttr.FrmID, md.No);

                        //遍历属性集合.
                        foreach (DataRow dr in dtMapAttr.Rows)
                        {
                            string keyOfEn = dr[MapAttrAttr.KeyOfEn].ToString();
                            foreach (FrmField ff in ffs)
                            {
                                if (ff.KeyOfEn.Equals(keyOfEn) == false)
                                    continue;

                                dr[MapAttrAttr.UIIsEnable] = ff.UIIsEnable;//是否只读?
                                dr[MapAttrAttr.UIVisible] = ff.UIVisible; //是否可见?
                                dr[MapAttrAttr.UIIsInput] = ff.ItIsNotNull; //是否必填?
                                dr[MapAttrAttr.IsSigan] = ff.ItIsSigan;


                                dr[MapAttrAttr.DefVal] = ff.DefVal; //默认值.

                                Attr attr = new Attr();
                                attr.MyDataType = DataType.AppString;
                                attr.DefaultValOfReal = ff.DefVal;
                                attr.Key = ff.KeyOfEn;

                                if (dr[MapAttrAttr.UIIsEnable].ToString() == "0")
                                    attr.UIIsReadonly = true;
                                else
                                    attr.UIIsReadonly = false;


                                //处理默认值.
                                if (DataType.IsNullOrEmpty(ff.DefVal) == true)
                                    continue;

                                //数据类型.
                                attr.MyDataType = int.Parse(dr[MapAttrAttr.MyDataType].ToString());
                                string v = ff.DefVal;

                                //设置默认值.
                                string myval = en.GetValStrByKey(ff.KeyOfEn);

                                // 设置默认值.
                                switch (ff.DefVal)
                                {
                                    case "@WebUser.No":
                                        if (attr.UIIsReadonly == true)
                                        {
                                            en.SetValByKey(attr.Key, WebUser.No);
                                        }
                                        else
                                        {
                                            if (DataType.IsNullOrEmpty(myval) || myval.Equals(v))
                                                en.SetValByKey(attr.Key, WebUser.No);
                                        }
                                        continue;
                                    case "@WebUser.Name":
                                        if (attr.UIIsReadonly == true)
                                        {
                                            en.SetValByKey(attr.Key, WebUser.Name);
                                        }
                                        else
                                        {
                                            if (DataType.IsNullOrEmpty(myval) || myval.Equals(v))
                                                en.SetValByKey(attr.Key, WebUser.Name);
                                        }
                                        continue;
                                    case "@WebUser.FK_Dept":
                                    case "@WebUser.DeptNo":
                                        if (attr.UIIsReadonly == true)
                                        {
                                            en.SetValByKey(attr.Key, WebUser.DeptNo);
                                        }
                                        else
                                        {
                                            if (DataType.IsNullOrEmpty(myval) || myval.Equals(v))
                                                en.SetValByKey(attr.Key, WebUser.DeptNo);
                                        }
                                        continue;
                                    case "@WebUser.FK_DeptName":
                                        if (attr.UIIsReadonly == true)
                                        {
                                            en.SetValByKey(attr.Key, WebUser.DeptName);
                                        }
                                        else
                                        {
                                            if (DataType.IsNullOrEmpty(myval) || myval.Equals(v))
                                                en.SetValByKey(attr.Key, WebUser.DeptName);
                                        }
                                        continue;
                                    case "@WebUser.FK_DeptNameOfFull":
                                    case "@WebUser.FK_DeptFullName":
                                        if (attr.UIIsReadonly == true)
                                        {
                                            en.SetValByKey(attr.Key, WebUser.DeptNameOfFull);
                                        }
                                        else
                                        {
                                            if (DataType.IsNullOrEmpty(myval) || myval.Equals(v))
                                                en.SetValByKey(attr.Key, WebUser.DeptNameOfFull);
                                        }
                                        continue;
                                    case "@RDT":
                                        if (attr.UIIsReadonly == true)
                                        {
                                            if (attr.MyDataType == DataType.AppDate || myval.Equals(v))
                                                en.SetValByKey(attr.Key, DataType.CurrentDate);

                                            if (attr.MyDataType == DataType.AppDateTime || myval.Equals(v))
                                                en.SetValByKey(attr.Key, DataType.CurrentDateTime);
                                        }
                                        else
                                        {
                                            if (DataType.IsNullOrEmpty(myval) || myval.Equals(v))
                                            {
                                                if (attr.MyDataType == DataType.AppDate)
                                                    en.SetValByKey(attr.Key, DataType.CurrentDate);
                                                else
                                                    en.SetValByKey(attr.Key, DataType.CurrentDateTime);
                                            }
                                        }
                                        continue;
                                    case "@yyyy年MM月dd日":
                                    case "@yyyy年MM月dd日HH时mm分":
                                    case "@yy年MM月dd日":
                                    case "@yy年MM月dd日HH时mm分":
                                        if (attr.UIIsReadonly == true)
                                        {
                                            en.SetValByKey(attr.Key, DateTime.Now.ToString(v.Replace("@", "")));
                                        }
                                        else
                                        {
                                            if (DataType.IsNullOrEmpty(myval) || myval.Equals(v))
                                                en.SetValByKey(attr.Key, DateTime.Now.ToString(v.Replace("@", "")));
                                        }
                                        continue;
                                    default:
                                        continue;
                                }
                            }
                        }

                        //改变他的属性. 不知道是否应该这样写？
                        ds.Tables.Remove("Sys_MapAttr");
                        ds.Tables.Add(dtMapAttr);

                        //处理radiobutton的模式的控件.
                    }
                    #endregion 自定义方案.

                }
                #endregion 处理权限方案s

                #region 加入主表的数据.
                //增加主表数据.
                DataTable mainTable = en.ToDataTableField("MainTable");
                ds.Tables.Add(mainTable);
                #endregion 加入主表的数据.
                // 执行表单事件. FrmLoadAfter .
                msg = ExecEvent.DoFrm(md, EventListFrm.FrmLoadAfter, en);
                if (DataType.IsNullOrEmpty(msg) == false)
                    return "err@错误:" + msg;
                string json = BP.Tools.Json.DataSetToJson(ds, false);


                //DataType.WriteFile("c:/aaa.txt", json);
                return json;
            }
            catch (Exception ex)
            {
                GEEntity myen = new GEEntity(this.EnsName);
                myen.CheckPhysicsTable();

                BP.Sys.CCFormAPI.RepareCCForm(this.EnsName);
                return "err@装载表单期间出现如下错误FrmGener_Init,ccform有自动诊断修复功能请在刷新一次，如果仍然存在请联系管理员. @" + ex.Message;
            }
        }
        public string FrmFreeReadonly_Init()
        {
            try
            {
                MapData md = new MapData(this.EnsName);
                DataSet ds = BP.Sys.CCFormAPI.GenerHisDataSet(md.No);

                #region 把主表数据放入.
                string atParas = "";
                //主表实体.
                GEEntityOID en = new GEEntityOID(this.EnsName);

                #region 求出 who is pk 值.
                Int64 pk = this.RefOID;
                if (pk == 0)
                    pk = this.OID;
                if (pk == 0)
                    pk = this.WorkID;

                if (this.NodeID != 0 && DataType.IsNullOrEmpty(this.FlowNo) == false)
                {
                    /*说明是流程调用它， 就要判断谁是表单的PK.*/
                    FrmNode fn = new FrmNode(this.NodeID, this.FrmID);
                    switch (fn.WhoIsPK)
                    {
                        case WhoIsPK.FID:
                            pk = this.FID;
                            if (pk == 0)
                                throw new Exception("@没有接收到参数FID");
                            break;
                        case WhoIsPK.PWorkID: /*父流程ID*/
                            pk = this.PWorkID;
                            if (pk == 0)
                                throw new Exception("@没有接收到参数PWorkID");
                            break;
                        case WhoIsPK.OID:
                        default:
                            break;
                    }
                }
                #endregion  求who is PK.

                en.OID = pk;
                en.RetrieveFromDBSources();

                //增加主表数据.
                DataTable mainTable = en.ToDataTableField(md.No);
                mainTable.TableName = "MainTable";

                ds.Tables.Add(mainTable);
                #endregion 把主表数据放入.

                return BP.Tools.Json.DataSetToJson(ds, false);
            }
            catch (Exception ex)
            {
                GEEntity myen = new GEEntity(this.EnsName);
                myen.CheckPhysicsTable();

                BP.Sys.CCFormAPI.RepareCCForm(this.EnsName);
                return "err@装载表单期间出现如下错误FrmFreeReadonly_Init,ccform有自动诊断修复功能请在刷新一次，如果仍然存在请联系管理员. @" + ex.Message;
            }
        }
        /// <summary>
        /// 执行保存
        /// </summary>
        /// <returns></returns>
        public string FrmGener_Save()
        {
            try
            {
                //保存主表数据.
                GEEntityOID en = new GEEntityOID(this.EnsName);

                #region 求出 who is pk 值.
                Int64 pk = this.RefOID;
                if (pk == 0)
                    pk = this.OID;
                if (pk == 0)
                    pk = this.WorkID;

                if (this.NodeID != 0 && DataType.IsNullOrEmpty(this.FlowNo) == false)
                {
                    /*说明是流程调用它， 就要判断谁是表单的PK.*/
                    FrmNode fn = new FrmNode(this.NodeID, this.FrmID);
                    switch (fn.WhoIsPK)
                    {
                        case WhoIsPK.FID:
                            pk = this.FID;
                            if (pk == 0)
                                throw new Exception("@没有接收到参数FID");
                            break;
                        case WhoIsPK.PWorkID: /*父流程ID*/
                            pk = this.PWorkID;
                            if (pk == 0)
                                throw new Exception("@没有接收到参数PWorkID");
                            break;
                        case WhoIsPK.OID:
                        default:
                            break;
                    }

                    if (fn.FrmSln == FrmSln.Readonly)
                    {
                        /*如果是不可以编辑*/
                        return "";
                    }
                }
                #endregion  求who is PK.

                en.SetValByKey("OID", pk);
                int i = en.RetrieveFromDBSources();
                en.ResetDefaultVal();

                try
                {
                    Hashtable ht = BP.Pub.PubClass.GetMainTableHT();
                    foreach (string item in ht.Keys)
                    {
                        en.SetValByKey(item, ht[item]);
                    }
                }
                catch (Exception ex)
                {
                    return "err@方法：MyBill_SaveIt错误，在执行  GetMainTableHT 期间" + ex.Message;
                }

                en.OID = pk;

                // 处理表单保存前事件.
                MapData md = new MapData(this.EnsName);

                #region 调用事件. 
                //是不是从表的保存.
                if (this.GetRequestValInt("IsForDtl") == 1)
                {
                    #region 从表保存前处理事件.
                    //获得主表事件.
                    FrmEvents fes = new FrmEvents(this.EnsName); //获得事件.
                    GEEntity mainEn = null;
                    if (fes.Count > 0)
                    {
                        string msg = fes.DoEventNode(BP.Sys.EventListFrm.DtlRowSaveBefore, en);
                        if (DataType.IsNullOrEmpty(msg) == false)
                            return "err@" + msg;
                    }

                    MapDtl mdtl = new MapDtl(this.EnsName);
                    if (mdtl.FEBD.Length != 0)
                    {
                        string str = mdtl.FEBD;
                        BP.Sys.Base.FormEventBaseDtl febd = BP.Sys.Base.Glo.GetFormDtlEventBaseByEnName(mdtl.No);

                        if (febd != null)
                        {
                            febd.HisEn = mdtl.GenerGEMainEntity(this.RefPKVal);
                            febd.HisEnDtl = en;

                            febd.DoIt(EventListFrm.DtlRowSaveBefore, febd.HisEn, en, null);
                        }
                    }
                    #endregion 从表保存前处理事件.
                }
                else
                {
                    ExecEvent.DoFrm(md, EventListFrm.SaveBefore, en);
                }
                #endregion 调用事件
                if (i == 0)
                    en.Insert();
                else
                    en.Update();

                #region 调用事件.
                if (this.GetRequestValInt("IsForDtl") == 1)
                {
                    #region 从表保存前处理事件.
                    //获得主表事件.
                    FrmEvents fes = new FrmEvents(this.EnsName); //获得事件.
                    GEEntity mainEn = null;
                    if (fes.Count > 0)
                    {
                        string msg = fes.DoEventNode(EventListFrm.DtlRowSaveBefore, en);
                        if (DataType.IsNullOrEmpty(msg) == false)
                            return "err@" + msg;
                    }

                    MapDtl mdtl = new MapDtl(this.EnsName);

                    if (mdtl.FEBD.Length != 0)
                    {
                        string str = mdtl.FEBD;
                        BP.Sys.Base.FormEventBaseDtl febd = BP.Sys.Base.Glo.GetFormDtlEventBaseByEnName(mdtl.No);

                        if (febd != null)
                        {
                            febd.HisEn = mdtl.GenerGEMainEntity(this.RefPKVal);
                            febd.HisEnDtl = en;

                            febd.DoIt(EventListFrm.DtlRowSaveAfter, febd.HisEn, en, null);
                        }
                    }
                    #endregion 从表保存前处理事件.
                }
                else
                {
                    ExecEvent.DoFrm(md, EventListFrm.SaveAfter, en);
                }
                #endregion 调用事件

                return "保存成功.";
            }
            catch (Exception ex)
            {
                return "err@" + ex.Message;
            }
        }
        #endregion

        #region dtl.htm 从表.
        public string Dtl_ComputeForMainTable()
        {
            String oper = this.GetRequestVal("Oper");
            String ptable = this.GetRequestVal("PTable");
            String sql = "SELECT " + oper + "(" + this.KeyOfEn + ") From " + ptable + " WHERE RefPK='" + this.RefPKVal + "'";
            return DBAccess.RunSQLReturnString(sql);
        }
        /// <summary>
        /// 初始化从表数据
        /// </summary>
        /// <returns>返回结果数据</returns>
        public string Dtl_Init()
        {
            DataSet ds = Dtl_Init_Dataset();
            return BP.Tools.Json.DataSetToJson(ds, false);
        }
        private DataSet Dtl_Init_Dataset()
        {
            #region 组织参数.
            MapDtl mdtl = new MapDtl(this.EnsName);
            mdtl.No = this.EnsName;
            string dtlRefPKVal = this.RefPKVal; //从表的RefPK
                                               
            #region 如果是测试，就创建表.
            if (this.NodeID == 999999 || this.GetRequestVal("IsTest") != null)
            {
                GEDtl dtl = new GEDtl(mdtl.No);
                dtl.CheckPhysicsTable();
                dtlRefPKVal = "0";
            }
            #endregion 如果是测试，就创建表.

            string frmID = mdtl.FrmID;

            if (this.NodeID != 0 && this.NodeID != 999999)
                frmID = frmID.Replace("_" + this.NodeID, "");

            if (this.NodeID != 0
                && mdtl.FrmID.Equals("Temp") == false
                && this.NodeID != 999999)
            {
                Node nd = new BP.WF.Node(this.NodeID);

                Flow flow = new Flow(nd.FlowNo);

                BP.WF.Template.FrmNode fn = new BP.WF.Template.FrmNode(nd.NodeID, frmID);

                if (flow.FlowDevModel == FlowDevModel.JiJian || nd.HisFormType == NodeFormType.SheetTree
                    || nd.HisFormType == NodeFormType.RefOneFrmTree
                    || nd.HisFormType == NodeFormType.FoolTruck)
                {
                    /*如果
                     * 1,传来节点ID, 不等于0.
                     * 2,不是节点表单.  就要判断是否是独立表单，如果是就要处理权限方案。*/
                    if (fn.FrmSln == FrmSln.Readonly)
                    {
                        mdtl.ItIsInsert = false;
                        mdtl.ItIsDelete = false;
                        mdtl.ItIsUpdate = false;
                        mdtl.ItIsReadonly = true;
                    }

                    ///自定义权限.
                    if (fn.FrmSln == FrmSln.Self)
                    {
                        mdtl.No = this.EnsName + "_" + this.NodeID;
                        if (mdtl.RetrieveFromDBSources() == 0)
                        {
                            mdtl.No = this.EnsName;
                            /*如果设置了自定义方案，但是没有定义，从表属性，就需要去默认值. */
                        }
                    }

                }
                dtlRefPKVal = BP.WF.Dev2Interface.GetDtlRefPKVal(this.WorkID, this.PWorkID, this.FID, this.NodeID, frmID, mdtl);
                if (dtlRefPKVal.Equals("0") == true
                    || DataType.IsNullOrEmpty(dtlRefPKVal))
                    dtlRefPKVal = this.RefPKVal;
            }

            if (this.GetRequestValInt("IsReadonly") == 1)
            {
                mdtl.ItIsInsert = false;
                mdtl.ItIsDelete = false;
                mdtl.ItIsUpdate = false;
            }

            string strs = this.RequestParas;
            strs = strs.Replace("?", "@");
            strs = strs.Replace("&", "@");
            #endregion 组织参数.

            //获得他的描述,与数据.
            DataSet ds = BP.WF.CCFormAPI.GenerDBForCCFormDtl(frmID, mdtl, this.RefPKVal, strs, dtlRefPKVal, this.FID, this.GetRequestValBoolen("IshistoryData"), this.GetRequestVal("TrackID"));
            return ds;
        }
        public string Dtl_DataOfPage()
        {
            MapDtl mapDtl = new MapDtl(this.EnsName);
            string frmID = mapDtl.FrmID;
            string dtlRefPKVal = this.RefPKVal; //从表的RefPK
            if (this.NodeID != 0)
            {
                frmID = frmID.Replace("_" + this.NodeID, "");
                Node nd = new Node(this.NodeID);
                FrmNode fn = new FrmNode(nd.NodeID, frmID);
                if (fn.FrmSln == FrmSln.Self)
                {
                    mapDtl.No = this.EnsName + "_" + this.NodeID;
                    if (mapDtl.RetrieveFromDBSources() == 0)
                    {
                        mapDtl.No = this.EnsName;
                        mapDtl.RetrieveFromDBSources();
                    }
                }
                dtlRefPKVal = Dev2Interface.GetDtlRefPKVal(this.WorkID, this.PWorkID, this.FID, this.NodeID, frmID, mapDtl);
                if (dtlRefPKVal.Equals("0") == true || DataType.IsNullOrEmpty(dtlRefPKVal))
                {
                    dtlRefPKVal = this.RefPKVal;
                }
            }
            //实体.
            Entity en = null;
            MapData mapData = new MapData(frmID);
            if (mapData.EntityType == EntityType.FrmEntityNoName)
            {
                en = new GEEntityNoName(frmID);
                en.SetValByKey("No", this.RefPKVal);
                if (en.RetrieveFromDBSources() == 0)
                {
                    en.SetValByKey("No", dtlRefPKVal);
                    en.RetrieveFromDBSources();
                }
            }
            else
            {
                en = new GEEntity(frmID);
                en.SetValByKey("OID", Int64.Parse(this.RefPKVal));
                if (en.RetrieveFromDBSources() == 0)
                {
                    en.SetValByKey("OID", Int64.Parse(dtlRefPKVal));
                    en.RetrieveFromDBSources();
                }
            }
            string orderField = this.GetRequestVal("OrderField");
            string orderBy = this.GetRequestVal("OrderBy");
            return BP.WF.CCFormAPI.GetDtlInfoByPage(mapDtl, en, dtlRefPKVal, this.PageIdx, this.GetRequestVal("paras"), orderField, orderBy);
        }
        /// <summary>
        /// 上传发发票.
        /// </summary>
        /// <returns></returns>
        public string Dtl_Invoice()
        {
            #region 获取上传的文件并转成Base64
            if (HttpContextHelper.RequestFilesCount == 0)
                return "err@请上传需要识别的发票";
            //判断上传的图片格式
            var files = HttpContextHelper.RequestFiles();
            string fileName = System.IO.Path.GetFileName(files[0].FileName);
            string ext = fileName.Substring(fileName.LastIndexOf(".") + 1);
            string exts = "PNG、JPG、JPEG、BMP";
            if (exts.Contains(ext.ToUpper()) == false)
                return "err@上传的文件格式有误,请上传" + exts + "格式的图片";
            Stream stream = HttpContextHelper.RequestFiles(0).OpenReadStream();//new MemoryStream();
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            string base64 = Convert.ToBase64String(bytes);
            stream.Close();
            #endregion 获取上传的文件

            #region 百度云识别发票
            string token = BP.WF.Port.BaiDuOCR.BaiDu.getAccessToken();
            JsonData jd = JsonMapper.ToObject(token);
            string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/multiple_invoice?access_token=" + jd["access_token"].ToString();
            Encoding encoding = Encoding.Default;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
            request.Method = "post";
            request.KeepAlive = true;
            String str = "&image=" + HttpUtility.UrlEncode(base64);
            byte[] buffer = encoding.GetBytes(str);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string result = reader.ReadToEnd();
            if (result.Contains("error_code"))
            {
                JObject resultObj = JObject.Parse(result);
                string code = resultObj.GetValue("error_code").ToString();
                string msg = resultObj.GetValue("error_msg").ToString();
                return "err@发票识别错误，错误码：" + code + "错误信息：" + msg + "，请您在[百度云官方API](https://cloud.baidu.com/doc/OCR/s/dk3h7y5vr)中根据错误码查询错误原因";
            }
            #endregion 百度云识别发票

            #region 开始保存数据.
            //JObject jObj = new JObject(result);
            result = JObject.Parse(result).GetValue("words_result").ToString();
            JArray json = JArray.Parse(result);
            GEDtl dtl = new GEDtl(this.FrmID);
            dtl.SetValByKey("RefPK", this.RefPKVal);
            foreach (JObject item in json)
            {
                string resultStr = item.GetValue("result").ToString();
                JObject resultObj = JObject.Parse(resultStr);
                //发票类型
                string type = item.GetValue("type").ToString();
                string typeStr = "";
                switch (type)
                {
                    case "vat_invoice":
                        string strObj = resultObj.GetValue("InvoiceType").ToString();
                        typeStr = JArray.Parse(strObj)[0].Value<string>("word");
                        //获取发票代码   
                        string invoiceCode = resultObj.GetValue("InvoiceCode").ToString();
                        if (JArray.Parse(invoiceCode).Count == 0)
                            dtl.SetValByKey("InvoiceID", "");
                        else
                            dtl.SetValByKey("InvoiceID", JArray.Parse(invoiceCode)[0].Value<string>("word"));
                        // 发票号码  
                        string invoiceNum = resultObj.GetValue("InvoiceNum").ToString();
                        if (JArray.Parse(invoiceNum).Count == 0)
                            dtl.SetValByKey("InvoiceCode", "");
                        else
                            dtl.SetValByKey("InvoiceCode", JArray.Parse(invoiceNum)[0].Value<string>("word"));
                        //开票日期
                        string invoiceDate = resultObj.GetValue("InvoiceDate").ToString();
                        if (JArray.Parse(invoiceDate).Count == 0)
                            dtl.SetValByKey("InvoiceRelDT", "");
                        else
                            dtl.SetValByKey("InvoiceRelDT", JArray.Parse(invoiceDate)[0].Value<string>("word"));
                        //发票金额(价税合计)
                        string amountInFiguers = resultObj.GetValue("AmountInFiguers").ToString();
                        if (JArray.Parse(amountInFiguers).Count == 0)
                            dtl.SetValByKey("InvoiceJE", "");
                        else
                            dtl.SetValByKey("InvoiceJE", JArray.Parse(amountInFiguers)[0].Value<string>("word"));
                        //税额
                        string commodityTax = resultObj.GetValue("CommodityTax").ToString();
                        if (JArray.Parse(commodityTax).Count == 0)
                            dtl.SetValByKey("InvoiceTaxJE", "");
                        else
                            dtl.SetValByKey("InvoiceTaxJE", JArray.Parse(commodityTax)[0].Value<string>("word"));
                        // 使用的税率
                        string commodityTaxRate = resultObj.GetValue("CommodityTaxRate").ToString();
                        if (JArray.Parse(commodityTaxRate).Count == 0)
                            dtl.SetValByKey("InvoiceTaxRate", "");
                        else
                            dtl.SetValByKey("InvoiceTaxRate", JArray.Parse(commodityTaxRate)[0].Value<string>("word"));
                        //购买方名称
                        string purchaserName = resultObj.GetValue("PurchaserName").ToString();
                        if (JArray.Parse(purchaserName).Count == 0)
                            dtl.SetValByKey("InvoiceBuyName", "");
                        else
                            dtl.SetValByKey("InvoiceBuyName", JArray.Parse(purchaserName)[0].Value<string>("word"));
                        //购买方ID
                        string purchaserRegisterNum = resultObj.GetValue("PurchaserRegisterNum").ToString();
                        if (JArray.Parse(purchaserRegisterNum).Count == 0)
                            dtl.SetValByKey("InvoiceBuyCode", "");
                        else
                            dtl.SetValByKey("InvoiceBuyCode", JArray.Parse(purchaserRegisterNum)[0].Value<string>("word"));
                        //购买方地址电话
                        string purchaserAddress = resultObj.GetValue("PurchaserAddress").ToString();
                        if (JArray.Parse(purchaserAddress).Count == 0)
                            dtl.SetValByKey("InvoiceBuyAddr", "");
                        else
                            dtl.SetValByKey("InvoiceBuyAddr", JArray.Parse(purchaserAddress)[0].Value<string>("word"));
                        //购买方开户行及账号
                        string purchaserBank = resultObj.GetValue("PurchaserBank").ToString();
                        if (JArray.Parse(purchaserBank).Count == 0)
                            dtl.SetValByKey("InvoiceBuyBankInfo", "");
                        else
                            dtl.SetValByKey("InvoiceBuyBankInfo", JArray.Parse(purchaserBank)[0].Value<string>("word"));
                        //销售方名称
                        string sellerName = resultObj.GetValue("SellerName").ToString();
                        if (JArray.Parse(sellerName).Count == 0)
                            dtl.SetValByKey("InvoiceSaleName", "");
                        else
                            dtl.SetValByKey("InvoiceSaleName", JArray.Parse(sellerName)[0].Value<string>("word"));
                        //销售方ID
                        string sellerRegisterNum = resultObj.GetValue("SellerRegisterNum").ToString();
                        if (JArray.Parse(sellerRegisterNum).Count == 0)
                            dtl.SetValByKey("InvoiceSaleCode", "");
                        else
                            dtl.SetValByKey("InvoiceSaleCode", JArray.Parse(sellerRegisterNum)[0].Value<string>("word"));
                        //销售方地址电话
                        string sellerAddress = resultObj.GetValue("SellerAddress").ToString();
                        if (JArray.Parse(sellerAddress).Count == 0)
                            dtl.SetValByKey("InvoiceSaleAddr", "");
                        else
                            dtl.SetValByKey("InvoiceSaleAddr", JArray.Parse(sellerAddress)[0].Value<string>("word"));
                        //销售方开户行及账号
                        string sellerBank = resultObj.GetValue("SellerBank").ToString();
                        if (JArray.Parse(sellerBank).Count == 0)
                            dtl.SetValByKey("InvoiceSaleBankInfo", "");
                        else
                            dtl.SetValByKey("InvoiceSaleBankInfo", JArray.Parse(sellerBank)[0].Value<string>("word"));
                        break;
                    case "taxi_receipt":
                        typeStr = "出租车票";
                        break;
                    case "train_ticket":
                        typeStr = "火车票";
                        //车票号
                        string ticket_num = JArray.Parse(resultObj.GetValue("ticket_num").ToString())[0].Value<string>("word");
                        //始发站
                        string starting_station = JArray.Parse(resultObj.GetValue("starting_station").ToString())[0].Value<string>("word");
                        //车次号
                        string train_num = JArray.Parse(resultObj.GetValue("train_num").ToString())[0].Value<string>("word");
                        //到达站
                        string destination_station = JArray.Parse(resultObj.GetValue("destination_station").ToString())[0].Value<string>("word");
                        //出发日期
                        string ticket_date = JArray.Parse(resultObj.GetValue("date").ToString())[0].Value<string>("word");
                        //车票金额
                        string ticket_rates = JArray.Parse(resultObj.GetValue("ticket_rates").ToString())[0].Value<string>("word");
                        //席别
                        string seat_category = JArray.Parse(resultObj.GetValue("seat_category").ToString())[0].Value<string>("word");
                        //乘客姓名
                        string ticket_name = JArray.Parse(resultObj.GetValue("name").ToString())[0].Value<string>("word");
                        //身份证号
                        string id_num = JArray.Parse(resultObj.GetValue("ID_card").ToString())[0].Value<string>("word");
                        //序列号
                        string serial_number = JArray.Parse(resultObj.GetValue("serial_number").ToString())[0].Value<string>("word");
                        //售站
                        string sales_station = JArray.Parse(resultObj.GetValue("sales_station").ToString())[0].Value<string>("word");

                        //发票代码
                        dtl.SetValByKey("InvoiceID", ticket_num);
                        // 发票号码
                        string invoice_num = resultObj.GetValue("invoice_num").ToString();
                        if (JArray.Parse(invoice_num).Count == 0)
                            dtl.SetValByKey("InvoiceCode", invoice_num);
                        else
                            dtl.SetValByKey("InvoiceCode", JArray.Parse(resultObj.GetValue("invoice_num").ToString())[0].Value<string>("word"));
                        //开票日期
                        string invoice_date = resultObj.GetValue("invoice_date").ToString();
                        if (JArray.Parse(invoice_date).Count == 0)
                            dtl.SetValByKey("InvoiceRelDT", invoice_date);
                        else
                            dtl.SetValByKey("InvoiceRelDT", JArray.Parse(resultObj.GetValue("invoice_date").ToString())[0].Value<string>("word"));
                        //不含税金额
                        string fare = JArray.Parse(resultObj.GetValue("fare").ToString())[0].Value<string>("word");

                        //发票金额(价税合计)
                        dtl.SetValByKey("InvoiceJE", ticket_rates);
                        //税额
                        string tax = resultObj.GetValue("tax").ToString();
                        if (JArray.Parse(tax).Count == 0)
                            dtl.SetValByKey("InvoiceTaxJE", invoice_date);
                        else
                            dtl.SetValByKey("InvoiceTaxJE", JArray.Parse(resultObj.GetValue("tax").ToString())[0].Value<string>("word"));
                        // 使用的税率
                        string tax_rate = resultObj.GetValue("tax_rate").ToString();
                        if (JArray.Parse(tax_rate).Count == 0)
                            dtl.SetValByKey("InvoiceTaxRate", tax_rate);
                        else
                            dtl.SetValByKey("InvoiceTaxRate", JArray.Parse(resultObj.GetValue("tax_rate").ToString())[0].Value<string>("word"));
                        //购买方名称
                        dtl.SetValByKey("InvoiceBuyName", ticket_name);
                        //购买方ID
                        dtl.SetValByKey("InvoiceBuyCode", id_num);
                        //销售方名称
                        dtl.SetValByKey("InvoiceSaleName", sales_station);
                        //获取其他信息
                        dtl.SetValByKey("Note", ticket_name + "乘坐" + train_num + "列车于" + ticket_date + "出发,从" + starting_station + "到" + destination_station + "");
                        break;
                    case "quota_invoice":
                        typeStr = "定额发票";
                        break;
                    case "air_ticket":
                        typeStr = "飞机行程单";
                        String name = JArray.Parse(resultObj.GetValue("name").ToString())[0].Value<string>("word");
                        String carrier = JArray.Parse(resultObj.GetValue("carrier").ToString())[0].Value<string>("word");
                        String date = JArray.Parse(resultObj.GetValue("date").ToString())[0].Value<string>("word");
                        //发票金额(价税合计)
                        dtl.SetValByKey("InvoiceJE", JArray.Parse(resultObj.GetValue("ticket_rates").ToString())[0].Value<string>("word"));
                        dtl.SetValByKey("InvoiceRelDT", date);
                        dtl.SetValByKey("InvoiceBuyName", name);
                        dtl.SetValByKey("InvoiceSaleName", carrier);
                        dtl.SetValByKey("InvoiceSaleCode", JArray.Parse(resultObj.GetValue("agent_code").ToString())[0].Value<string>("word"));
                        //获取其他信息
                        String startingStation = JArray.Parse(resultObj.GetValue("starting_station").ToString())[0].Value<string>("word");
                        String destinationStation = JArray.Parse(resultObj.GetValue("destination_station").ToString())[0].Value<string>("word");
                        String time = JArray.Parse(resultObj.GetValue("time").ToString())[0].Value<string>("word");
                        String flight = JArray.Parse(resultObj.GetValue("flight").ToString())[0].Value<string>("word");
                        dtl.SetValByKey("Note", name + "乘坐" + carrier + flight + date + " " + time + "从" + startingStation + "到" + destinationStation + "出发");

                        break;
                    case "roll_normal_invoice":
                        typeStr = "卷票";
                        break;
                    case "printed_invoice":
                        typeStr = "机打发票";
                        break;
                    case "bus_ticket":
                        typeStr = "汽车票";
                        break;
                    case "toll_invoice":
                        typeStr = "过路过桥费发票";
                        break;
                    case "ferry_ticket":
                        typeStr = "船票";
                        break;
                    case "motor_vehicle_invoice":
                        typeStr = "机动车销售发票";
                        break;
                    case "used_vehicle_invoice":
                        typeStr = "二手车发票";
                        break;
                    case "taxi_online_ticket":
                        typeStr = "网约车行程单";
                        break;
                    case "limit_invoice":
                        typeStr = "限额发票";
                        break;
                    case "shopping_receipt":
                        typeStr = "购物小票";
                        break;
                    case "pos_invoice":
                        typeStr = "POS小票";
                        break;
                    default:
                        typeStr = "POS小票";
                        break;
                }
                dtl.SetValByKey("InvoiceTypeStr", typeStr);

            }

            dtl.InsertAsOID(DBAccess.GenerOID(dtl.EnMap.PhysicsTable));
            #endregion 开始保存数据.
            return "";
        }
        /// <summary>
        /// 执行从表的保存.
        /// </summary>
        /// <returns></returns>
        public string Dtl_Save()
        {
            MapDtl mdtl = new MapDtl(this.EnsName);
            string dtlRefPKVal = this.RefPKVal;
            string fk_mapDtl = this.MapDtlNo;
            FrmEvents fes = new FrmEvents(this.EnsName); //获得事件.
            GEEntity mainEn = null;
            //获取集合
            string str = this.GetRequestVal("Json");
            if (DataType.IsNullOrEmpty(str) == true)
                return "不需要保存";

            #region 从表保存前处理事件.
            if (fes.Count > 0)
            {
                mainEn = mdtl.GenerGEMainEntity(this.RefPKVal);
                string msg = fes.DoEventNode(EventListFrm.DtlRowSaveBefore, mainEn);
                if (msg != null)
                    throw new Exception(msg);
            }
            #endregion 从表保存前处理事件.

            #region 处理权限方案。
            if (this.NodeID != 0 && this.NodeID != 999999)
            {
                Node nd = new Node(this.NodeID);
                if (nd.HisFormType == NodeFormType.SheetTree || nd.HisFormType == NodeFormType.RefOneFrmTree)
                {
                    FrmNode fn = new FrmNode(nd.NodeID, this.FrmID);
                    if (fn.FrmSln == FrmSln.Self)
                    {
                        string no = fk_mapDtl + "_" + nd.NodeID;
                        MapDtl mdtlSln = new MapDtl();
                        mdtlSln.No = no;
                        int result = mdtlSln.RetrieveFromDBSources();
                        if (result != 0)
                        {
                            mdtl = mdtlSln;
                            fk_mapDtl = no;
                        }
                    }
                }

                dtlRefPKVal = BP.WF.Dev2Interface.GetDtlRefPKVal(this.WorkID, this.PWorkID, this.FID, this.NodeID, this.FrmID, mdtl);
                if (dtlRefPKVal.Equals("0") == true)
                    dtlRefPKVal = this.RefPKVal;
            }
            #endregion 处理权限方案。

            #region 保存的业务逻辑.
            GEDtls dtls = new GEDtls(this.EnsName);
            GEDtl dtl = dtls.GetNewEntity as GEDtl;
            Attrs attrs = dtl.EnMap.Attrs;
            JArray json = JArray.Parse(str);
            int idx = 0;
            foreach (JObject item in json)
            {
                JToken result = item.GetValue("OID");
                Int64 oid = 0;
                if (result != null)
                    oid = Int64.Parse(result.ToString());
                dtl = dtls.GetNewEntity as GEDtl;
                foreach (Attr attr in attrs)
                {
                    if (attr.ItIsRefAttr == true)
                        continue;
                    string val = item.GetValue(attr.Field) != null ? item.GetValue(attr.Field).ToString() : "";
                    if (attr.MyDataType == DataType.AppDateTime || attr.MyDataType == DataType.AppDate)
                    {
                        //if (attr.UIIsReadonly == true)
                        //    continue;
                        dtl.SetValByKey(attr.Key, val);
                        continue;
                    }
                    if (attr.UIContralType == UIContralType.CheckBok && attr.MyDataType == DataType.AppInt)
                    {
                        if (val.Equals("0"))
                            dtl.SetValByKey(attr.Key, 0);
                        else
                            dtl.SetValByKey(attr.Key, 1);
                        continue;
                    }
                    dtl.SetValByKey(attr.Key, val);
                    continue;
                }
                dtl.SetValByKey("OID", oid);
                //关联主赋值.
                dtl.RefPK = dtlRefPKVal;
                if (this.FID == 0 && DataType.IsNumStr(dtl.RefPK))
                    dtl.FID = Int64.Parse(dtl.RefPK);
                else
                    dtl.FID = this.FID;

                //如果是新建，并且里面是ForFID的模式.
                //(已经是启用FID模式，就是子线程节点不用赋新值了先注释了)
                //if (mdtl.DtlOpenType == DtlOpenType.ForFID)
                //    dtl.FID = Int64.Parse(dtlRefPKVal);
                dtl.Rec = WebUser.No;
                dtl.SetValByKey("Idx", idx);
                if (oid == 0)
                {
                    dtl.InsertAsOID(DBAccess.GenerOID(mdtl.PTable));
                    //处理从表行数据插入成功后，更新FrmEleDB中数据
                    string dbstr = SystemConfig.AppCenterDBVarStr;
                    Paras paras = new Paras();
                    /* if (BP.Difference.SystemConfig.AppCenterDBType == DBType.Oracle || BP.Difference.SystemConfig.AppCenterDBType == DBType.PostgreSQL || BP.Difference.SystemConfig.AppCenterDBType == DBType.UX || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR3 || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR6)
                         paras.SQL = "UPDATE Sys_FrmEleDB SET RefPKVal=" + dbstr + "RefPKVal,MyPK=EleID||'_'||" + dtl.OID.ToString() + "||'_'||Tag1  WHERE RefPKVal=" + dbstr + "OldRefPKVal";
                     else if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MySQL)
                         paras.SQL = "UPDATE Sys_FrmEleDB SET RefPKVal=" + dbstr + "RefPKVal,MyPK=CONCAT(EleID,'_'," + dtl.OID.ToString() + ",'_',Tag1)  WHERE RefPKVal=" + dbstr + "OldRefPKVal";
                     else
                         paras.SQL = "UPDATE Sys_FrmEleDB SET RefPKVal=" + dbstr + "RefPKVal,MyPK= EleID+'_'+'" + dtl.OID.ToString() + "'+'_'+Tag1  WHERE RefPKVal=" + dbstr + "OldRefPKVal";*/
                    paras.Add("RefPKVal", dtl.OID.ToString());
                    paras.Add("OldRefPKVal", this.RefPKVal + "_" + idx);
                    DBAccess.RunSQL(paras);
                    //处理从表行数据插入成功后，更新FrmAttachmentDB中数据
                    paras.SQL = "UPDATE Sys_FrmAttachmentDB SET RefPKVal=" + dbstr + "RefPKVal  WHERE RefPKVal=" + dbstr + "OldRefPKVal";
                    DBAccess.RunSQL(paras);
                    idx++;
                    continue;
                }
                dtl.Update();
                idx++;
            }
            if (fes.Count > 0)
            {
                string msg = fes.DoEventNode(EventListFrm.DtlRowSaveAfter, mainEn);
                if (msg != null)
                    throw new Exception(msg);
            }
            #endregion 保存的业务逻辑.
            DataTable dt = CCFormAPI.GetDtlInfo(mdtl, mainEn, dtlRefPKVal);

            return BP.Tools.Json.ToJson(dt);
        }
        /// <summary>
        /// 从表移动
        /// </summary>
        /// <returns></returns>
        public string DtlList_Move()
        {
            int newIdx = this.GetRequestValInt("newIdx");
            int oldIdx = this.GetRequestValInt("oldIdx");
            Int64 newOID = this.GetRequestValInt("newOID");
            Int64 oldOID = this.GetRequestValInt("oldOID");
            //处理从表行数据插入成功后，更新FrmAttachmentDB中数据
            Paras paras = new Paras();
            paras.Add("FK_MapData", this.FrmID);

            string dbstr = SystemConfig.AppCenterDBVarStr;
            string athSQL = "UPDATE Sys_FrmAttachmentDB SET RefPKVal=" + dbstr + "RefPKVal  WHERE RefPKVal=" + dbstr + "OldRefPKVal AND FK_MapData=" + dbstr + "FK_MapData";
            string eleSQL = "UPDATE Sys_FrmEleDB SET RefPKVal=" + dbstr + "RefPKVal  WHERE RefPKVal=" + dbstr + "OldRefPKVal AND FK_MapData=" + dbstr + "FK_MapData";

            if (newOID == 0 && oldOID == 0)
            {
                //先改变oldID的附件信息
                /*paras.Add("RefPKVal", this.RefPKVal + "_" + oldIdx + "_"+ oldIdx);
                paras.Add("OldRefPKVal", this.RefPKVal + "_" + oldIdx);
                paras.SQL = athSQL;
                DBAccess.RunSQL(paras);
                paras.SQL = eleSQL;
                DBAccess.RunSQL(paras);*/

                paras.Add("RefPKVal", this.RefPKVal + "_" + newIdx + "_" + newIdx);
                paras.Add("OldRefPKVal", this.RefPKVal + "_" + oldIdx);
                paras.SQL = athSQL;
                DBAccess.RunSQL(paras);
                paras.SQL = eleSQL;
                DBAccess.RunSQL(paras);
                paras.Add("RefPKVal", this.RefPKVal + "_" + oldIdx + "_" + oldIdx);
                paras.Add("OldRefPKVal", this.RefPKVal + "_" + newIdx);
                paras.SQL = athSQL;
                DBAccess.RunSQL(paras);
                paras.SQL = eleSQL;
                DBAccess.RunSQL(paras);
                return "从表拖拽完成";
            }
            if (newOID == 0)
            {
                paras.Add("RefPKVal", this.RefPKVal + "_" + newIdx + "_" + newIdx);
                paras.Add("OldRefPKVal", this.RefPKVal + "_" + oldIdx);
                paras.SQL = athSQL;
                DBAccess.RunSQL(paras);
                paras.SQL = eleSQL;
                DBAccess.RunSQL(paras);

            }
            if (oldOID == 0)
            {
                paras.Add("RefPKVal", this.RefPKVal + "_" + oldIdx + "_" + oldIdx);
                paras.Add("OldRefPKVal", this.RefPKVal + "_" + newIdx);
            }
            paras.SQL = athSQL;
            DBAccess.RunSQL(paras);
            paras.SQL = eleSQL;
            DBAccess.RunSQL(paras);
            return "从表拖拽完成";
        }

        public string DtlList_MoveAfter()
        {
            Paras paras = new Paras();
            paras.Add("FK_MapData", this.FrmID);
            int newIdx = this.GetRequestValInt("newIdx");
            string dbstr = SystemConfig.AppCenterDBVarStr;
            string athSQL = "";
            string eleSQL = "";
            switch (SystemConfig.AppCenterDBType)
            {
                case DBType.MSSQL:
                    athSQL = "UPDATE Sys_FrmAttachmentDB SET RefPKVal=SUBSTRING(RefPKVal,0,CHARINDEX('_', RefPKVal, CHARINDEX('_', RefPKVal) + 1))  WHERE RefPKVal LIKE " + dbstr + "RefPKVal+'[_]%[_]%' AND FK_MapData=" + dbstr + "FK_MapData";
                    eleSQL = "UPDATE Sys_FrmEleDB SET RefPKVal=SUBSTRING(RefPKVal,0,CHARINDEX('_', RefPKVal, CHARINDEX('_', RefPKVal) + 1))  WHERE RefPKVal LIKE " + dbstr + "RefPKVal+'[_]%[_]%' AND FK_MapData=" + dbstr + "FK_MapData";
                    break;
                case DBType.Oracle:
                case DBType.KingBaseR3:
                case DBType.KingBaseR6:
                case DBType.DM:
                case DBType.GBASE8CByOracle:
                    athSQL = "UPDATE Sys_FrmAttachmentDB SET RefPKVal=SUBSTR(RefPKVal,0,INSTR(RefPKVal,'_',1,2))  WHERE RefPKVal LIKE " + dbstr + "RefPKVal||'\\_%\\_%'  ESCAPE '\\' AND FK_MapData=" + dbstr + "FK_MapData";
                    eleSQL = "UPDATE Sys_FrmEleDB SET RefPKVal=SUBSTR(RefPKVal,0,INSTR(RefPKVal,'_',1,2))  WHERE RefPKVal LIKE " + dbstr + "RefPKVal||'\\_%\\_%'  ESCAPE '\\' AND FK_MapData=" + dbstr + "FK_MapData";
                    break;
                case DBType.MySQL:
                    athSQL = "UPDATE Sys_FrmAttachmentDB SET RefPKVal=SUBSTRING(RefPKVal,1,LOCATE('_', RefPKVal, LOCATE('_', RefPKVal)+1)-1)  WHERE RefPKVal LIKE CONCAT(" + dbstr + "RefPKVal,'\\\\_%\\\\_%') AND FK_MapData=" + dbstr + "FK_MapData";
                    eleSQL = "UPDATE Sys_FrmEleDB SET RefPKVal=SUBSTRING(RefPKVal,1,LOCATE('_', RefPKVal, LOCATE('_', RefPKVal)+1)-1)  WHERE RefPKVal LIKE CONCAT(" + dbstr + "RefPKVal,'\\\\_%\\\\_%') AND FK_MapData=" + dbstr + "FK_MapData";
                    break;
                case DBType.PostgreSQL:
                case DBType.HGDB:
                    athSQL = "UPDATE Sys_FrmAttachmentDB SET RefPKVal=SUBSTRING(RefPKVal,1,CHAR_LENGTH(RefPKVal)-POSITION('_' in REVERSE(RefPKVal)))  WHERE RefPKVal LIKE CONCAT(" + dbstr + "RefPKVal,'!_%!_%')  ESCAPE '!' AND FK_MapData=" + dbstr + "FK_MapData";
                    eleSQL = "UPDATE Sys_FrmEleDB SET RefPKVal=SUBSTRING(RefPKVal,1,CHAR_LENGTH(RefPKVal)-POSITION('_' in REVERSE(RefPKVal)))  WHERE RefPKVal LIKE CONCAT(" + dbstr + "RefPKVal,'!_%!_%')  ESCAPE '!' AND FK_MapData=" + dbstr + "FK_MapData";
                    break;
                default:
                    return "err@" + SystemConfig.AppCenterDBType + "还未解析";
            }
            paras.Add("RefPKVal", this.RefPKVal);
            paras.SQL = athSQL;
            DBAccess.RunSQL(paras);
            paras.SQL = eleSQL;
            DBAccess.RunSQL(paras);
            return "";
        }
        /// <summary>
        /// 导出excel与附件信息,并且压缩一个压缩包.
        /// </summary>
        /// <returns>返回下载路径</returns>
        public string Dtl_ExpZipFiles()
        {
            DataSet ds = Dtl_Init_Dataset();

            return "err@尚未wancheng.";
        }
        /// <summary>
        /// 保存单行数据
        /// </summary>
        /// <returns></returns>
        public string Dtl_SaveRow()
        {
            //从表.
            string fk_mapDtl = this.MapDtlNo;
            string RowIndex = this.GetRequestVal("RowIndex");
            MapDtl mdtl = new MapDtl(fk_mapDtl);
            string dtlRefPKVal = this.RefPKVal;

            string isRead = mdtl.Row.GetValByKey(MapDtlAttr.IsReadonly).ToString();
            //string isRead = this.GetRequestVal("IsReadonly");
            if (DataType.IsNullOrEmpty(isRead) == false && "1".Equals(isRead) == true)
                return "";

            #region 处理权限方案。
            if (this.NodeID != 0 && this.NodeID != 999999 && mdtl.No.Contains("ND") == false)
            {
                Node nd = new Node(this.NodeID);
                if (nd.HisFormType == NodeFormType.SheetTree || nd.HisFormType == NodeFormType.RefOneFrmTree)
                {
                    FrmNode fn = new FrmNode(nd.NodeID, this.FrmID);
                    if (fn.FrmSln == FrmSln.Self)
                    {
                        string no = fk_mapDtl + "_" + nd.NodeID;
                        MapDtl mdtlSln = new MapDtl();
                        mdtlSln.No = no;
                        int result = mdtlSln.RetrieveFromDBSources();
                        if (result != 0)
                        {
                            mdtl = mdtlSln;
                            fk_mapDtl = no;
                        }
                    }
                }

                dtlRefPKVal = BP.WF.Dev2Interface.GetDtlRefPKVal(this.WorkID, this.PWorkID, this.FID, this.NodeID, this.FrmID, mdtl);
                if (dtlRefPKVal.Equals("0") == true)
                    dtlRefPKVal = this.RefPKVal;
            }
            #endregion 处理权限方案。

            if (mdtl.ItIsReadonly == true)
                return "err@不允许保存.IsReadonly=1," + mdtl.Name + " ID:" + mdtl.No;

            if (mdtl.ItIsInsert == false && mdtl.ItIsUpdate == false)
                return "err@不允许保存. IsInsert=false,IsUpdate=false " + mdtl.Name + " ID:" + mdtl.No;

            //从表实体.
            GEDtl dtl = new GEDtl(fk_mapDtl);
            int oid = this.RefOID;
            if (oid != 0)
            {
                dtl.OID = oid;
                dtl.RetrieveFromDBSources();
            }
            else
            {
                //关联主赋值.
                dtl.RefPK = dtlRefPKVal;
                if (this.FID == 0)
                    dtl.FID = Int64.Parse(dtl.RefPK);
                else
                    dtl.FID = this.FID;

                //如果是新建，并且里面是ForFID的模式.
                if (mdtl.DtlOpenType == DtlOpenType.ForFID)
                    dtl.FID = Int64.Parse(dtlRefPKVal);
            }

            #region 给实体循环赋值/并保存.
            BP.En.Attrs attrs = dtl.EnMap.Attrs;
            foreach (BP.En.Attr attr in attrs)
            {
                if (attr.Key.Equals("FID") == true || attr.Key.Equals("RefPK") == true)
                    continue;

                dtl.SetValByKey(attr.Key, this.GetRequestVal(attr.Key));

            }
            dtl.SetValByKey("Idx", RowIndex);
            #region 从表保存前处理事件.
            //获得主表事件.
            FrmEvents fes = new FrmEvents(fk_mapDtl); //获得事件.
            GEEntity mainEn = null;
            if (fes.Count > 0)
            {
                mainEn = mdtl.GenerGEMainEntity(this.RefPKVal);
                string msg = fes.DoEventNode(EventListFrm.DtlRowSaveBefore, mainEn);
                if (DataType.IsNullOrEmpty(msg) == false)
                    return "err@" + msg;
            }

            if (mdtl.FEBD.Length != 0)
            {
                string str = mdtl.FEBD;
                BP.Sys.Base.FormEventBaseDtl febd = BP.Sys.Base.Glo.GetFormDtlEventBaseByEnName(mdtl.No);

                febd.HisEn = mdtl.GenerGEMainEntity(this.RefPKVal);
                febd.HisEnDtl = dtl;

                febd.DoIt(EventListFrm.DtlRowSaveBefore, febd.HisEn, dtl, null);
            }
            #endregion 从表保存前处理事件.

            //一直找不到refpk  值为null .
            if (DataType.IsNullOrEmpty(dtl.RefPK) == true)
                dtl.RefPK = this.RefPKVal;

            if (dtl.OID == 0)
            {
                try
                {
                    dtl.InsertAsOID(DBAccess.GenerOID(mdtl.PTable));
                }
                catch (Exception ex)
                {
                    return "err@在保存行:" + RowIndex + " 错误: " + ex.Message;
                }
                bool isVue3 = this.GetRequestValBoolen("IsVue3");
                if (isVue3 == false)
                {
                    //dtl生成oid后，将pop弹出的FrmEleDB表中的数据用oid替换掉
                    string refval = this.RefPKVal + "_" + RowIndex;
                    //处理从表行数据插入成功后，更新FrmEleDB中数据
                    string dbstr = SystemConfig.AppCenterDBVarStr;
                    Paras paras = new Paras();
                    if (BP.Difference.SystemConfig.AppCenterDBType == DBType.Oracle || BP.Difference.SystemConfig.AppCenterDBType == DBType.GBASE8CByOracle || BP.Difference.SystemConfig.AppCenterDBType == DBType.PostgreSQL || BP.Difference.SystemConfig.AppCenterDBType == DBType.HGDB || BP.Difference.SystemConfig.AppCenterDBType == DBType.UX || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR3 || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR6 || DBAccess.AppCenterDBType == DBType.DM)
                        paras.SQL = "UPDATE Sys_FrmEleDB SET RefPKVal=" + dbstr + "RefPKVal,MyPK=EleID||'_'||" + dtl.OID.ToString() + "||'_'||Tag1  WHERE RefPKVal=" + dbstr + "OldRefPKVal AND FK_MapData=" + dbstr + "FK_MapData";
                    else if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MySQL)
                        paras.SQL = "UPDATE Sys_FrmEleDB SET RefPKVal=" + dbstr + "RefPKVal,MyPK=CONCAT(EleID,'_'," + dtl.OID.ToString() + ",'_',Tag1)  WHERE RefPKVal=" + dbstr + "OldRefPKVal AND FK_MapData=" + dbstr + "FK_MapData";
                    else
                        paras.SQL = "UPDATE Sys_FrmEleDB SET RefPKVal=" + dbstr + "RefPKVal,MyPK= EleID+'_'+'" + dtl.OID.ToString() + "'+'_'+Tag1  WHERE RefPKVal=" + dbstr + "OldRefPKVal AND FK_MapData=" + dbstr + "FK_MapData";
                    paras.Add("RefPKVal", dtl.OID.ToString());
                    paras.Add("OldRefPKVal", refval);
                    paras.Add("FK_MapData", fk_mapDtl);
                    DBAccess.RunSQL(paras);
                    //处理从表行数据插入成功后，更新FrmAttachmentDB中数据
                    paras.SQL = "UPDATE Sys_FrmAttachmentDB SET RefPKVal=" + dbstr + "RefPKVal  WHERE RefPKVal=" + dbstr + "OldRefPKVal AND FK_MapData=" + dbstr + "FK_MapData";
                    DBAccess.RunSQL(paras);
                }
                if (isVue3 == true)
                {
                    //把从表中的子表RefPK=0的改成当前从表的OID
                    MapDtls childDtls = new MapDtls(mdtl.No);
                    foreach (MapDtl childDtl in childDtls)
                    {
                        DBAccess.RunSQL("UPDATE " + childDtl.PTable + " SET RefPK='" + dtl.OID + "' WHERE RefPK='" + this.RefPKVal + "'");
                    }
                }

            }
            else
            {
                dtl.Update();
            }
            #endregion 给实体循环赋值/并保存.

            #region 从表保存后处理事件。
            //页面定义的事件.
            if (fes.Count > 0)
            {
                string msg = fes.DoEventNode(BP.Sys.EventListFrm.DtlRowSaveAfter, mainEn);
                if (DataType.IsNullOrEmpty(msg) == false)
                    return "err@" + msg;
            }
            //事件实体类.
            if (mdtl.FEBD.Length != 0)
            {
                string str = mdtl.FEBD;
                BP.Sys.Base.FormEventBaseDtl febd = BP.Sys.Base.Glo.GetFormDtlEventBaseByEnName(mdtl.No);

                febd.HisEn = mdtl.GenerGEMainEntity(this.RefPKVal);
                febd.HisEnDtl = dtl;
                febd.DoIt(EventListFrm.DtlRowSaveAfter, febd.HisEn, dtl, null);
            }
            #endregion 处理事件.

            //返回当前数据存储信息.
            return dtl.ToJson();
        }


        public string Dtl_ChangePopAndAthIdx()
        {
            int oldRowIdx = GetRequestValInt("oldRowIdx");
            int newRowIdx = GetRequestValInt("newRowIdx");
            //dtl生成oid后，将pop弹出的FrmEleDB表中的数据用oid替换掉
            string refval = this.WorkID + "_" + oldRowIdx;
            string newRefVal = this.WorkID + "_" + newRowIdx;
            //处理从表行数据插入成功后，更新FrmEleDB中数据
            string dbstr = SystemConfig.AppCenterDBVarStr;
            Paras paras = new Paras();
            if (BP.Difference.SystemConfig.AppCenterDBType == DBType.Oracle || BP.Difference.SystemConfig.AppCenterDBType == DBType.GBASE8CByOracle || BP.Difference.SystemConfig.AppCenterDBType == DBType.PostgreSQL || BP.Difference.SystemConfig.AppCenterDBType == DBType.HGDB || BP.Difference.SystemConfig.AppCenterDBType == DBType.UX || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR3 || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR6 || DBAccess.AppCenterDBType == DBType.DM)
                paras.SQL = "UPDATE Sys_FrmEleDB SET RefPKVal=" + dbstr + "RefPKVal,MyPK=EleID||'_'||'" + newRefVal + "'||'_'||Tag1  WHERE RefPKVal=" + dbstr + "OldRefPKVal";
            else if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MySQL)
                paras.SQL = "UPDATE Sys_FrmEleDB SET RefPKVal=" + dbstr + "RefPKVal,MyPK=CONCAT(EleID,'_','" + newRefVal + "','_',Tag1)  WHERE RefPKVal=" + dbstr + "OldRefPKVal";
            else
                paras.SQL = "UPDATE Sys_FrmEleDB SET RefPKVal=" + dbstr + "RefPKVal,MyPK= EleID+'_'+'" + newRefVal + "'+'_'+Tag1  WHERE RefPKVal=" + dbstr + "OldRefPKVal";
            paras.Add("RefPKVal", newRefVal);
            paras.Add("OldRefPKVal", refval);
            DBAccess.RunSQL(paras);
            //处理从表行数据插入成功后，更新FrmAttachmentDB中数据
            paras.SQL = "UPDATE Sys_FrmAttachmentDB SET RefPKVal=" + dbstr + "RefPKVal  WHERE RefPKVal=" + dbstr + "OldRefPKVal AND FK_MapData=" + dbstr + "FK_MapData";
            paras.Add("FK_MapData", this.FrmID);
            DBAccess.RunSQL(paras);
            return "执行成功";
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public string Dtl_DeleteRow()
        {
            GEDtl dtl = new GEDtl(this.MapDtlNo);
            dtl.OID = this.RefOID;

            #region 从表 删除 前处理事件.
            //获得主表事件.
            FrmEvents fes = new FrmEvents(this.MapDtlNo); //获得事件.
            GEEntity mainEn = null;
            if (fes.Count > 0)
            {
                string msg = fes.DoEventNode(EventListFrm.DtlRowDelBefore, dtl);
                if (DataType.IsNullOrEmpty(msg) == false)
                    return "err@" + msg;
            }

            MapDtl mdtl = new MapDtl(this.MapDtlNo);
            if (mdtl.FEBD.Length != 0)
            {
                string str = mdtl.FEBD;
                BP.Sys.Base.FormEventBaseDtl febd = BP.Sys.Base.Glo.GetFormDtlEventBaseByEnName(mdtl.No);
                if (febd != null)
                {
                    febd.HisEn = mdtl.GenerGEMainEntity(this.RefPKVal);
                    febd.HisEnDtl = dtl;
                    febd.DoIt(EventListFrm.DtlRowDelBefore, febd.HisEn, dtl, null);
                }
            }
            #endregion 从表 删除 前处理事件.

            //执行删除.
            dtl.Delete();

            #region 从表 删除 后处理事件.
            //获得主表事件.
            fes = new FrmEvents(this.MapDtlNo); //获得事件.
            if (fes.Count > 0)
            {
                string msg = fes.DoEventNode(EventListFrm.DtlRowDelAfter, dtl);
                if (DataType.IsNullOrEmpty(msg) == false)
                    return "err@" + msg;
            }

            if (mdtl.FEBD.Length != 0)
            {
                string str = mdtl.FEBD;
                BP.Sys.Base.FormEventBaseDtl febd = BP.Sys.Base.Glo.GetFormDtlEventBaseByEnName(mdtl.No);
                if (febd != null)
                {
                    febd.HisEn = mdtl.GenerGEMainEntity(this.RefPKVal);
                    febd.HisEnDtl = dtl;

                    febd.DoIt(EventListFrm.DtlRowDelAfter, febd.HisEn, dtl, null);
                }
            }

            #endregion 从表 删除 后处理事件.

            //如果有pop，删除相关存储
            FrmEleDBs FrmEleDBs = new FrmEleDBs();
            QueryObject qo = new QueryObject(FrmEleDBs);
            qo.AddWhere(FrmEleDBAttr.FK_MapData, this.MapDtlNo);
            qo.addAnd();
            qo.AddWhere(FrmEleDBAttr.RefPKVal, Convert.ToString(dtl.OID));
            qo.DoQuery();
            if (FrmEleDBs != null && FrmEleDBs.Count > 0)
            {
                foreach (FrmEleDB FrmEleDB in FrmEleDBs)
                {
                    FrmEleDB.Delete();
                }
            }
            //如果可以上传附件这删除相应的附件信息
            FrmAttachmentDBs dbs = new FrmAttachmentDBs();
            dbs.Delete(FrmAttachmentDBAttr.FK_MapData, this.MapDtlNo, FrmAttachmentDBAttr.RefPKVal, this.RefOID);
            return "删除成功";
        }
        public string Dtl_DeleteAllRow()
        {
            MapDtl dtl = new MapDtl(this.MapDtlNo);
            DBAccess.RunSQL("DELETE FROM " + dtl.PTable + " WHERE RefPK='" + this.RefPKVal + "'");
            return "删除成功";
        }
        /// <summary>
        /// 重新获取单个ddl数据
        /// </summary>
        /// <returns></returns>
        public string Dtl_ReloadDdl()
        {
            string Doc = this.GetRequestVal("Doc");
            DataTable dt = DBAccess.RunSQLReturnTable(Doc);
            dt.TableName = "ReloadDdl";
            return BP.Tools.Json.ToJson(dt);
        }
        #endregion dtl.htm 从表.

        public String DtlFrmGener_Init()
        {
            DataSet myds = new DataSet();

            MapDtl dtl = new MapDtl(this.EnsName);

            DataTable Sys_MapDtl = dtl.ToDataTableField("Sys_MapDtl");
            myds.Tables.Add(Sys_MapDtl);

            //明细表的表单描述
            MapAttrs attrs = dtl.MapAttrs;
            DataTable Sys_MapAttr = attrs.ToDataTableField("Sys_MapAttr");
            myds.Tables.Add(Sys_MapAttr);

            //明细表的配置信息.
            MapExts mes = dtl.MapExts;
            DataTable Sys_MapExt = mes.ToDataTableField("Sys_MapExt");
            myds.Tables.Add(Sys_MapExt);

            //从表的字段附件
            FrmAttachments aths = dtl.FrmAttachments;
            DataTable athDt = aths.ToDataTableField("Sys_FrmAttachment");
            myds.Tables.Add(athDt);

            //#region 把从表的- 外键表/枚举 加入 DataSet.

            MapExt me = null;
            DataTable ddlTable = new DataTable();
            ddlTable.Columns.Add("No");
            foreach (MapAttr attr in attrs)
            {

                //没有绑定外键
                string uiBindKey = attr.UIBindKey;
                if (DataType.IsNullOrEmpty(uiBindKey) == true)
                    continue;

                //#region 枚举字段
                if (attr.LGType == FieldTypeS.Enum)
                {
                    // 如果是枚举值, 判断是否存在.
                    if (myds.Tables.Contains(uiBindKey) == true)
                        continue;
                    string mysql = "SELECT IntKey AS No, Lab as Name FROM " + BP.Sys.Base.Glo.SysEnum() + " WHERE EnumKey='" + uiBindKey + "' ORDER BY IntKey ";
                    DataTable dtEnum = DBAccess.RunSQLReturnTable(mysql);
                    dtEnum.TableName = uiBindKey;

                    dtEnum.Columns[0].ColumnName = "No";
                    dtEnum.Columns[1].ColumnName = "Name";

                    myds.Tables.Add(dtEnum);
                    continue;
                }

                // 检查是否有下拉框自动填充。
                string keyOfEn = attr.KeyOfEn;

                me = (MapExt)mes.GetEntityByKey(MapExtAttr.ExtType, MapExtXmlList.AutoFullDLL,
                MapExtAttr.AttrOfOper, keyOfEn);
                if (me != null && me.DoWay.Equals("0") == false && myds.Tables.Contains(uiBindKey) == false) //是否存在.
                {
                    String fullSQL = me.Doc;
                    fullSQL = fullSQL.Replace("~", "'");
                    fullSQL = BP.WF.Glo.DealExp(fullSQL, null);

                    if (DataType.IsNullOrEmpty(fullSQL) == true)
                        throw new Exception("err@没有给AutoFullDLL配置SQL：MapExt：=" + me.MyPK + ",原始的配置SQL为:" + me.Doc);

                    DataTable dt = DBAccess.RunSQLReturnTable(fullSQL);

                    if (uiBindKey.ToLower().Equals("blank"))
                        dt.TableName = keyOfEn;
                    else
                        dt.TableName = uiBindKey;

                    if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel == FieldCaseModel.UpperCase)
                    {
                        if (dt.Columns.Contains("NO") == true)
                            dt.Columns["NO"].ColumnName = "No";
                        if (dt.Columns.Contains("NAME") == true)
                            dt.Columns["NAME"].ColumnName = "Name";
                        if (dt.Columns.Contains("PARENTNO") == true)
                            dt.Columns["PARENTNO"].ColumnName = "ParentNo";
                    }

                    if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel == FieldCaseModel.Lowercase)
                    {
                        if (dt.Columns.Contains("no") == true)
                            dt.Columns["no"].ColumnName = "No";
                        if (dt.Columns.Contains("name") == true)
                            dt.Columns["name"].ColumnName = "Name";
                        if (dt.Columns.Contains("parentno") == true)
                            dt.Columns["parentno"].ColumnName = "ParentNo";
                    }

                    myds.Tables.Add(dt);
                    continue;
                }


                if (myds.Tables.Contains(uiBindKey) == true)
                    continue;

                // 获得数据.
                DataTable mydt = BP.Pub.PubClass.GetDataTableByUIBineKey(uiBindKey, null);
                if (uiBindKey.ToLower().Equals("blank"))
                    mydt.TableName = keyOfEn;

                if (mydt == null)
                {
                    DataRow ddldr = ddlTable.NewRow();
                    ddldr["No"] = uiBindKey;
                    ddlTable.Rows.Add(ddldr);
                }
                else
                {
                    myds.Tables.Add(mydt);
                }
                //#endregion 外键字段
            }
            ddlTable.TableName = "UIBindKey";
            myds.Tables.Add(ddlTable);

            //放入一个空白的实体，用与获取默认值.
            GEDtl dtlBlank = new GEDtl(dtl.No);
            dtlBlank.ResetDefaultVal();
            myds.Tables.Add(dtlBlank.ToDataTableField("Blank"));

            return BP.Tools.Json.ToJson(myds);
        }

        #region dtl.Card
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public string DtlCard_Init()
        {
            DataSet ds = new DataSet();

            MapDtl dtl = new MapDtl(this.EnsName);
            string dtlRefPKVal = this.RefPKVal;
            if (this.NodeID != 0 && dtl.FrmID != "Temp"
               && this.EnsName.Contains("ND" + this.NodeID) == false
               && this.NodeID != 999999)
            {
                Node nd = new BP.WF.Node(this.NodeID);

                Flow flow = new Flow(nd.FlowNo);

                BP.WF.Template.FrmNode fn = new BP.WF.Template.FrmNode(nd.NodeID, dtl.FrmID);

                if (flow.FlowDevModel == FlowDevModel.JiJian || nd.HisFormType == NodeFormType.SheetTree
                    || nd.HisFormType == NodeFormType.RefOneFrmTree
                    || nd.HisFormType == NodeFormType.FoolTruck)
                {
                    /*如果
                     * 1,传来节点ID, 不等于0.
                     * 2,不是节点表单.  就要判断是否是独立表单，如果是就要处理权限方案。*/
                    if (fn.FrmSln == FrmSln.Readonly)
                    {
                        dtl.ItIsInsert = false;
                        dtl.ItIsDelete = false;
                        dtl.ItIsUpdate = false;
                        dtl.ItIsReadonly = true;
                    }

                    ///自定义权限.
                    if (fn.FrmSln == FrmSln.Self)
                    {
                        dtl.No = this.EnsName + "_" + this.NodeID;
                        if (dtl.RetrieveFromDBSources() == 0)
                        {
                            dtl.No = this.EnsName;
                        }
                    }

                }
                dtlRefPKVal = BP.WF.Dev2Interface.GetDtlRefPKVal(this.WorkID, this.PWorkID, this.FID, this.NodeID, dtl.FrmID, dtl);
                if (dtlRefPKVal.Equals("0") == true
                    || DataType.IsNullOrEmpty(dtlRefPKVal))
                    dtlRefPKVal = this.RefPKVal;
            }

            //主表数据.
            DataTable dt = dtl.ToDataTableField("Main");
            ds.Tables.Add(dt);

            //主表字段.
            MapAttrs attrs = dtl.MapAttrs;
            ds.Tables.Add(attrs.ToDataTableField("MapAttrs"));

            //从表.
            MapDtls dtls = dtl.MapDtls;
            ds.Tables.Add(dtls.ToDataTableField("MapDtls"));

            //从表的从表.
            foreach (MapDtl en in dtls)
            {
                MapAttrs subAttrs = new MapAttrs(en.No);
                ds.Tables.Add(subAttrs.ToDataTableField(en.No));
            }

            //从表的数据.
            //GEDtls enDtls = new GEDtls(this.EnsName);
            #region  把从表的数据放入.
            GEDtls enDtls = new GEDtls(dtl.No);
            QueryObject qo = null;
            try
            {
                qo = new QueryObject(enDtls);
                switch (dtl.DtlOpenType)
                {
                    case DtlOpenType.ForEmp:  // 按人员来控制.
                        qo.AddWhere(dtl.DtlOpenField, dtlRefPKVal);
                        qo.addAnd();
                        qo.AddWhere(GEDtlAttr.Rec, WebUser.No);
                        break;
                    case DtlOpenType.ForWorkID: // 按工作ID来控制
                    case DtlOpenType.ForPWorkID:
                    case DtlOpenType.ForFID: // 按工作ID来控制
                    case DtlOpenType.ForPFID:
                        qo.AddWhere(dtl.DtlOpenField, dtlRefPKVal);
                        break;
                    case DtlOpenType.ForWorkIDAndSpecEmpNo: // 按照指定的字段是当前登录人员账号计算.
                        qo.AddWhere(dtl.DtlOpenField, dtlRefPKVal);
                        qo.addAnd();
                        string attr = dtl.GetParaString("DtlOpenPara");
                        if (DataType.IsNullOrEmpty(attr) == true)
                            throw new Exception("err@当前数据显示规则按照ForWorkIDAndSpecEmpNo计算，但是您没有设置人员账号字段名称,");
                        qo.AddWhere(attr, WebUser.No);
                        break;
                    case DtlOpenType.ForWorkIDAndOpenPara:
                        string attr1 = dtl.GetParaString("DtlOpenPara");
                        if (DataType.IsNullOrEmpty(attr1) == true)
                            throw new Exception("err@按照WorkID-计算并指定查询的字段计算，但是您没有选择字段,");
                        qo.AddWhere(attr1, dtlRefPKVal);
                        break;
                    default:
                        qo.AddWhere(dtl.DtlOpenField, dtlRefPKVal);
                        break;
                }
               
            }
            catch (Exception ex)
            {
                dtls.GetNewEntity.CheckPhysicsTable();
                throw ex;
            }

            //条件过滤.
            if (DataType.IsNullOrEmpty(dtl.FilterSQLExp) == false)
            {
                string[] strs = dtl.FilterSQLExp.Split('=');
                qo.addAnd();
                qo.AddWhere(strs[0], strs[1]);
            }

            //排序.
            if (DataType.IsNullOrEmpty(dtl.OrderBySQLExp) == false)
            {
                qo.addOrderBy(dtl.OrderBySQLExp);
            }
            else
            {
                //增加排序.
                qo.addOrderBy(GEDtlAttr.OID);
            }


            //从表
            DataTable dtDtl = qo.DoQueryToTable();
            dtDtl.TableName = "DTDtls";
            ds.Tables.Add(dtDtl);
            #endregion
            //enDtls.Retrieve(GEDtlAttr.RefPK, this.RefPKVal);
            //ds.Tables.Add(enDtls.ToDataTableField("DTDtls"));

            return BP.Tools.Json.ToJson(ds);

        }
        public string Dtl_InitCount()
        {
            MapDtl mapDtl = new MapDtl(this.EnsName);
            //实体.
            GEEntityOID en = new GEEntityOID(mapDtl.FrmID);
            en.OID = this.WorkID;
            if (en.RetrieveFromDBSources() == 0)
                en.Insert();
            String dtlRefPKVal = BP.WF.Dev2Interface.GetDtlRefPKVal(this.WorkID, this.PWorkID, this.FID, this.NodeID, mapDtl.FrmID, mapDtl);
            if (dtlRefPKVal.Equals("0") == true
                    || DataType.IsNullOrEmpty(dtlRefPKVal))
                dtlRefPKVal = this.RefPKVal;
            //获取初始化数据，不存在就创建
            int count = BP.WF.CCFormAPI.GetDtlCount(mapDtl, en, dtlRefPKVal, false);
            return count.ToString();
        }
        /// <summary>
        /// 获得从表的从表数据
        /// </summary>
        /// <returns></returns>
        public string DtlCard_Init_Dtl()
        {
            DataSet ds = new DataSet();

            MapDtl md = new MapDtl(this.EnsName);

            //主表数据.
            DataTable dt = md.ToDataTableField("Main");
            ds.Tables.Add(dt);

            //主表字段.
            MapAttrs attrs = md.MapAttrs;
            ds.Tables.Add(attrs.ToDataTableField("MapAttrs"));

            GEDtls enDtls = new GEDtls(this.EnsName);
            enDtls.Retrieve(GEDtlAttr.RefPK, this.RefPKVal);
            ds.Tables.Add(enDtls.ToDataTableField("DTDtls"));

            return BP.Tools.Json.ToJson(ds);
        }
        #endregion dtl.Card

        #region 保存手写签名图片
        /// <summary>
        /// 保存手写签名图片
        /// </summary>
        /// <returns>返回保存结果</returns>
        public string HandWriting_Save()
        {
            try
            {
                string basePath = BP.Difference.SystemConfig.PathOfDataUser + "HandWritingImg";
                string ny = DateTime.Now.ToString("yyyy_MM");
                string tempPath = basePath + "/" + ny + "/" + this.NodeID + "/";
                string tempName = this.WorkID + "_" + this.KeyOfEn + "_" + DateTime.Now.ToString("HHmmss") + ".png";

                if (System.IO.Directory.Exists(tempPath) == false)
                    System.IO.Directory.CreateDirectory(tempPath);
                //删除改目录下WORKID的文件
                string[] files = Directory.GetFiles(tempPath);
                foreach (string file in files)
                {
                    if (file.Contains(this.WorkID + "_" + this.KeyOfEn) == true)
                        System.IO.File.Delete(file);
                }
                string pic_Path = tempPath + tempName;

                string imgData = this.GetValFromFrmByKey("imageData");

                using (System.IO.FileStream fs = new FileStream(pic_Path, FileMode.Create))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        byte[] data = Convert.FromBase64String(imgData);
                        bw.Write(data);
                        bw.Close();
                    }
                }
                return "info@" + pic_Path.Replace("\\", "/"); ;
            }
            catch (Exception e)
            {
                return "err@" + e.Message;
            }
        }
        /// <summary>
        /// 图片转二进制流
        /// </summary>
        /// <returns></returns>
        public string ImageDatabytes()
        {
            string FilePath = BP.Difference.SystemConfig.PathOfDataUser + GetRequestVal("src");
            if (!File.Exists(FilePath))
                return "";
            Bitmap myBitmap = new Bitmap(Image.FromFile(FilePath));

            using (MemoryStream curImageStream = new MemoryStream())
            {
                myBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Png);
                curImageStream.Flush();

                byte[] bmpBytes = curImageStream.ToArray();
                //如果转字符串的话
                string BmpStr = Convert.ToBase64String(bmpBytes);
                return BmpStr;
            }
        }

        #endregion



        /// <summary>
        /// 处理SQL的表达式.
        /// </summary>
        /// <param name="exp">表达式</param>
        /// <returns>从from里面替换的表达式.</returns>
        public string DealExpByFromVals(string exp)
        {
            foreach (string strKey in HttpContextHelper.RequestParamKeys)
            {
                if (exp.Contains("@") == false)
                    return exp;
                string str = strKey.Replace("TB_", "").Replace("CB_", "").Replace("DDL_", "").Replace("RB_", "");

                exp = exp.Replace("@" + str, HttpContextHelper.RequestParams(strKey));
            }
            return exp;
        }
        /// <summary>
        /// 初始化树的接口
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string PopVal_InitTree()
        {
            string mypk = this.GetRequestVal("FK_MapExt");

            MapExt me = new MapExt();
            me.setMyPK(mypk);
            me.Retrieve();

            //获得配置信息.
            Hashtable ht = me.PopValToHashtable();
            DataTable dtcfg = BP.Pub.PubClass.HashtableToDataTable(ht);

            string parentNo = this.GetRequestVal("ParentNo");
            if (parentNo == null)
                parentNo = me.PopValTreeParentNo;

            DataSet resultDs = new DataSet();
            string sqlObjs = me.PopValTreeSQL;
            sqlObjs = sqlObjs.Replace("@WebUser.No", BP.Web.WebUser.No);
            sqlObjs = sqlObjs.Replace("@WebUser.Name", BP.Web.WebUser.Name);
            sqlObjs = sqlObjs.Replace("@WebUser.FK_Dept", BP.Web.WebUser.DeptNo);
            sqlObjs = sqlObjs.Replace("@WebUser.DeptNo", BP.Web.WebUser.DeptNo);

            sqlObjs = sqlObjs.Replace("@ParentNo", parentNo);
            sqlObjs = this.DealExpByFromVals(sqlObjs);

            DataTable dt = DBAccess.RunSQLReturnTable(sqlObjs);
            dt.TableName = "DTObjs";

            //判断是否是oracle.
            if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel == FieldCaseModel.UpperCase)
            {
                dt.Columns["NO"].ColumnName = "No";
                dt.Columns["NAME"].ColumnName = "Name";
                dt.Columns["PARENTNO"].ColumnName = "ParentNo";
            }
            if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel == FieldCaseModel.Lowercase)
            {
                dt.Columns["no"].ColumnName = "No";
                dt.Columns["name"].ColumnName = "Name";
                dt.Columns["parentno"].ColumnName = "ParentNo";
            }

            resultDs.Tables.Add(dt);

            //doubleTree
            if (me.PopValWorkModel == PopValWorkModel.TreeDouble && parentNo != me.PopValTreeParentNo)
            {
                sqlObjs = me.PopValDoubleTreeEntitySQL;
                sqlObjs = sqlObjs.Replace("@WebUser.No", BP.Web.WebUser.No);
                sqlObjs = sqlObjs.Replace("@WebUser.Name", BP.Web.WebUser.Name);
                sqlObjs = sqlObjs.Replace("@WebUser.FK_Dept", BP.Web.WebUser.DeptNo);
                sqlObjs = sqlObjs.Replace("@WebUser.DeptNo", BP.Web.WebUser.DeptNo);

                sqlObjs = sqlObjs.Replace("@ParentNo", parentNo);
                sqlObjs = this.DealExpByFromVals(sqlObjs);

                DataTable entityDt = DBAccess.RunSQLReturnTable(sqlObjs);
                entityDt.TableName = "DTEntitys";
                resultDs.Tables.Add(entityDt);


                //判断是否是oracle.
                if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel == FieldCaseModel.UpperCase)
                {
                    entityDt.Columns["NO"].ColumnName = "No";
                    entityDt.Columns["NAME"].ColumnName = "Name";
                }

                if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel == FieldCaseModel.Lowercase)
                {
                    entityDt.Columns["no"].ColumnName = "No";
                    entityDt.Columns["name"].ColumnName = "Name";

                }


            }

            return BP.Tools.Json.ToJson(resultDs);
        }

        /// <summary>
        /// 处理DataTable中的列名，将不规范的No,Name,ParentNo列纠正
        /// </summary>
        /// <param name="dt"></param>
        public void DoCheckTableColumnNameCase(DataTable dt)
        {
            foreach (DataColumn col in dt.Columns)
            {
                switch (col.ColumnName.ToLower())
                {
                    case "no":
                        col.ColumnName = "No";
                        break;
                    case "name":
                        col.ColumnName = "Name";
                        break;
                    case "parentno":
                        col.ColumnName = "ParentNo";
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 初始化PopVal的值   除了分页表格模式之外的其他数据值
        /// </summary>
        /// <returns></returns>
        public string PopVal_Init()
        {
            MapExt me = new MapExt();
            me.setMyPK(this.FK_MapExt);
            me.Retrieve();

            //数据对象，将要返回的.
            DataSet ds = new DataSet();

            //获得配置信息.
            Hashtable ht = me.PopValToHashtable();
            DataTable dtcfg = BP.Pub.PubClass.HashtableToDataTable(ht);

            //增加到数据源.
            ds.Tables.Add(dtcfg);

            if (me.PopValWorkModel == PopValWorkModel.SelfUrl)
                return "@SelfUrl" + me.PopValUrl;

            if (me.PopValWorkModel == PopValWorkModel.TableOnly)
            {
                string sqlObjs = me.PopValEntitySQL;
                sqlObjs = sqlObjs.Replace("@WebUser.No", BP.Web.WebUser.No);
                sqlObjs = sqlObjs.Replace("@WebUser.Name", BP.Web.WebUser.Name);
                sqlObjs = sqlObjs.Replace("@WebUser.FK_Dept", BP.Web.WebUser.DeptNo);
                sqlObjs = sqlObjs.Replace("@WebUser.DeptNo", BP.Web.WebUser.DeptNo);


                sqlObjs = this.DealExpByFromVals(sqlObjs);


                DataTable dt = DBAccess.RunSQLReturnTable(sqlObjs);
                dt.TableName = "DTObjs";
                DoCheckTableColumnNameCase(dt);
                ds.Tables.Add(dt);
                return BP.Tools.Json.ToJson(ds);
            }

            if (me.PopValWorkModel == PopValWorkModel.Group)
            {
                /*
                 *  分组的.
                 */

                string sqlObjs = me.PopValGroupSQL;
                if (sqlObjs.Length > 10)
                {
                    // sqlObjs = Glo.DealSQLExp(sqlObjs, null, null);

                    sqlObjs = sqlObjs.Replace("@WebUser.No", BP.Web.WebUser.No);
                    sqlObjs = sqlObjs.Replace("@WebUser.Name", BP.Web.WebUser.Name);
                    sqlObjs = sqlObjs.Replace("@WebUser.FK_Dept", BP.Web.WebUser.DeptNo);
                    sqlObjs = sqlObjs.Replace("@WebUser.DeptNo", BP.Web.WebUser.DeptNo);

                    sqlObjs = this.DealExpByFromVals(sqlObjs);


                    DataTable dt = DBAccess.RunSQLReturnTable(sqlObjs);
                    dt.TableName = "DTGroup";
                    DoCheckTableColumnNameCase(dt);
                    ds.Tables.Add(dt);
                }

                sqlObjs = me.PopValEntitySQL;
                if (sqlObjs.Length > 10)
                {
                    sqlObjs = sqlObjs.Replace("@WebUser.No", BP.Web.WebUser.No);
                    sqlObjs = sqlObjs.Replace("@WebUser.Name", BP.Web.WebUser.Name);
                    sqlObjs = sqlObjs.Replace("@WebUser.FK_Dept", BP.Web.WebUser.DeptNo);
                    sqlObjs = sqlObjs.Replace("@WebUser.DeptNo", BP.Web.WebUser.DeptNo);

                    sqlObjs = this.DealExpByFromVals(sqlObjs);


                    DataTable dt = DBAccess.RunSQLReturnTable(sqlObjs);
                    dt.TableName = "DTEntity";
                    DoCheckTableColumnNameCase(dt);
                    ds.Tables.Add(dt);
                }
                return BP.Tools.Json.ToJson(ds);

            }

            if (me.PopValWorkModel == PopValWorkModel.TablePage)
            {
                /* 分页的 */
                //key
                string key = this.GetRequestVal("Key");
                if (DataType.IsNullOrEmpty(key) == true)
                    key = "";

                //取出来查询条件.
                string[] conds = me.PopValSearchCond.Split('$');

                //pageSize
                string pageSize = this.GetRequestVal("pageSize");
                if (DataType.IsNullOrEmpty(pageSize))
                    pageSize = "10";

                //pageIndex
                string pageIndex = this.GetRequestVal("pageIndex");
                if (DataType.IsNullOrEmpty(pageIndex))
                    pageIndex = "1";

                string sqlObjs = me.PopValTablePageSQL;
                sqlObjs = sqlObjs.Replace("@WebUser.No", BP.Web.WebUser.No);
                sqlObjs = sqlObjs.Replace("@WebUser.Name", BP.Web.WebUser.Name);
                sqlObjs = sqlObjs.Replace("@WebUser.FK_Dept", BP.Web.WebUser.DeptNo);
                sqlObjs = sqlObjs.Replace("@WebUser.DeptNo", BP.Web.WebUser.DeptNo);

                sqlObjs = sqlObjs.Replace("@Key", key);

                //三个固定参数.
                sqlObjs = sqlObjs.Replace("@PageCount", ((int.Parse(pageIndex) - 1) * int.Parse(pageSize)).ToString());
                sqlObjs = sqlObjs.Replace("@PageSize", pageSize);
                sqlObjs = sqlObjs.Replace("@PageIndex", pageIndex);
                sqlObjs = this.DealExpByFromVals(sqlObjs);

                //替换其他参数.
                foreach (string cond in conds)
                {
                    if (cond == null || cond == "")
                        continue;

                    //参数.
                    string para = cond.Substring(5, cond.IndexOf("#") - 5);
                    string val = HttpContextHelper.RequestParams(para);
                    if (DataType.IsNullOrEmpty(val))
                    {
                        if (cond.Contains("ListSQL") == true || cond.Contains("EnumKey") == true)
                            val = "all";
                        else
                            val = "";
                    }
                    if (val == "all")
                    {
                        sqlObjs = sqlObjs.Replace(para + "=@" + para, "1=1");
                        sqlObjs = sqlObjs.Replace(para + "='@" + para + "'", "1=1");

                        int startIndex = 0;
                        while (startIndex != -1 && startIndex < sqlObjs.Length)
                        {
                            int index = sqlObjs.IndexOf("1=1", startIndex + 1);
                            if (index > 0 && sqlObjs.Substring(startIndex, index - startIndex).Trim().EndsWith("."))
                            {
                                int lastBlankIndex = sqlObjs.Substring(startIndex, index - startIndex).LastIndexOf(" ");


                                sqlObjs = sqlObjs.Remove(lastBlankIndex + startIndex + 1, index - lastBlankIndex - 1);

                                startIndex = (startIndex + lastBlankIndex) + 3;
                            }
                            else
                            {
                                startIndex = index;
                            }
                        }
                    }
                    else
                    {
                        //要执行两次替换有可能是，有引号.
                        sqlObjs = sqlObjs.Replace("@" + para, val);
                    }
                }


                DataTable dt = DBAccess.RunSQLReturnTable(sqlObjs);
                dt.TableName = "DTObjs";
                DoCheckTableColumnNameCase(dt);
                ds.Tables.Add(dt);

                //处理查询条件.
                //$Para=Dept#Label=所在班级#ListSQL=Select No,Name FROM Port_Dept WHERE No='@WebUser.No'
                //$Para=XB#Label=性别#EnumKey=XB
                //$Para=DTFrom#Label=注册日期从#DefVal=@Now-30
                //$Para=DTTo#Label=到#DefVal=@Now


                foreach (string cond in conds)
                {
                    if (DataType.IsNullOrEmpty(cond) == true)
                        continue;

                    string sql = null;
                    if (cond.Contains("#ListSQL=") == true)
                    {
                        sql = cond.Substring(cond.IndexOf("ListSQL") + 8);
                        sql = sql.Replace("@WebUser.No", WebUser.No);
                        sql = sql.Replace("@WebUser.Name", WebUser.Name);
                        sql = sql.Replace("@WebUser.FK_Dept", WebUser.DeptNo);
                        sql = sql.Replace("@WebUser.DeptNo", WebUser.DeptNo);
                        sql = this.DealExpByFromVals(sql);
                    }

                    if (cond.Contains("#EnumKey=") == true)
                    {
                        string enumKey = cond.Substring(cond.IndexOf("EnumKey") + 8);
                        sql = "SELECT IntKey AS No, Lab as Name FROM " + BP.Sys.Base.Glo.SysEnum() + " WHERE EnumKey='" + enumKey + "'";
                    }

                    //处理日期的默认值
                    //DefVal=@Now-30
                    //if (cond.Contains("@Now"))
                    //{
                    //    int nowIndex = cond.IndexOf(cond);
                    //    if (cond.Trim().Length - nowIndex > 5)
                    //    {
                    //        char optStr = cond.Trim()[nowIndex + 5];
                    //        int day = 0;
                    //        if (int.TryParse(cond.Trim().Substring(nowIndex + 6), out day)) {
                    //            cond = cond.Substring(0, nowIndex) + DateTime.Now.AddDays(-1 * day).ToString("yyyy-MM-dd HH:mm");
                    //        }
                    //    }
                    //}

                    if (sql == null)
                        continue;

                    //参数.
                    string para = cond.Substring(5, cond.IndexOf("#") - 5);
                    if (ds.Tables.Contains(para) == true)
                        throw new Exception("@配置的查询,参数名有冲突不能命名为:" + para);

                    //查询出来数据，就把他放入到dataset里面.
                    DataTable dtPara = DBAccess.RunSQLReturnTable(sql);
                    dtPara.TableName = para;
                    DoCheckTableColumnNameCase(dt);
                    ds.Tables.Add(dtPara); //加入到参数集合.
                }


                return BP.Tools.Json.ToJson(ds);
            }

            //返回数据.
            return BP.Tools.Json.ToJson(ds);
        }

        /// <summary>
        /// 初始化PopVal 分页表格模式的Count  杨玉慧
        /// </summary>
        /// <returns></returns>
        public string PopVal_InitTablePageCount()
        {
            MapExt me = new MapExt();
            me.setMyPK(this.FK_MapExt);
            me.Retrieve();

            //数据对象，将要返回的.
            DataSet ds = new DataSet();

            //获得配置信息.
            Hashtable ht = me.PopValToHashtable();
            DataTable dtcfg = BP.Pub.PubClass.HashtableToDataTable(ht);

            //增加到数据源.
            ds.Tables.Add(dtcfg);

            if (me.PopValWorkModel == PopValWorkModel.SelfUrl)
                return "@SelfUrl" + me.PopValUrl;
            if (me.PopValWorkModel == PopValWorkModel.TablePage)
            {
                /* 分页的 */
                //key
                string key = this.GetRequestVal("Key");
                if (DataType.IsNullOrEmpty(key) == true)
                    key = "";

                //取出来查询条件.
                string[] conds = me.PopValSearchCond.Split('$');

                string countSQL = me.PopValTablePageSQLCount;

                //固定参数.
                countSQL = countSQL.Replace("@WebUser.No", BP.Web.WebUser.No);
                countSQL = countSQL.Replace("@WebUser.Name", BP.Web.WebUser.Name);
                countSQL = countSQL.Replace("@WebUser.FK_Dept", BP.Web.WebUser.DeptNo);
                countSQL = countSQL.Replace("@Key", key);
                countSQL = this.DealExpByFromVals(countSQL);

                //替换其他参数.
                foreach (string cond in conds)
                {
                    if (cond == null || cond == "")
                        continue;

                    //参数.
                    string para = cond.Substring(5, cond.IndexOf("#") - 5);
                    string val = HttpContextHelper.RequestParams(para);
                    if (DataType.IsNullOrEmpty(val))
                    {
                        if (cond.Contains("ListSQL") == true || cond.Contains("EnumKey") == true)
                            val = "all";
                        else
                            val = "";
                    }

                    if (val == "all")
                    {
                        countSQL = countSQL.Replace(para + "=@" + para, "1=1");
                        countSQL = countSQL.Replace(para + "='@" + para + "'", "1=1");

                        //找到para 前面表的别名   如 t.1=1 把t. 去掉
                        int startIndex = 0;
                        while (startIndex != -1 && startIndex < countSQL.Length)
                        {
                            int index = countSQL.IndexOf("1=1", startIndex + 1);
                            if (index > 0 && countSQL.Substring(startIndex, index - startIndex).Trim().EndsWith("."))
                            {
                                int lastBlankIndex = countSQL.Substring(startIndex, index - startIndex).LastIndexOf(" ");


                                countSQL = countSQL.Remove(lastBlankIndex + startIndex + 1, index - lastBlankIndex - 1);

                                startIndex = (startIndex + lastBlankIndex) + 3;
                            }
                            else
                            {
                                startIndex = index;
                            }
                        }
                    }
                    else
                    {
                        //要执行两次替换有可能是，有引号.
                        countSQL = countSQL.Replace("@" + para, val);
                    }
                }

                string count = DBAccess.RunSQLReturnValInt(countSQL, 0).ToString();
                DataTable dtCount = new DataTable("DTCout");
                dtCount.TableName = "DTCout";
                dtCount.Columns.Add("Count", typeof(int));
                dtCount.Rows.Add(new[] { count });
                ds.Tables.Add(dtCount);


                //处理查询条件.
                //$Para=Dept#Label=所在班级#ListSQL=Select No,Name FROM Port_Dept WHERE No='@WebUser.No'
                //$Para=XB#Label=性别#EnumKey=XB
                //$Para=DTFrom#Label=注册日期从#DefVal=@Now-30
                //$Para=DTTo#Label=到#DefVal=@Now


                foreach (string cond in conds)
                {
                    if (DataType.IsNullOrEmpty(cond) == true)
                        continue;

                    string sql = null;
                    if (cond.Contains("#ListSQL=") == true)
                    {
                        sql = cond.Substring(cond.IndexOf("ListSQL") + 8);
                        sql = sql.Replace("@WebUser.No", WebUser.No);
                        sql = sql.Replace("@WebUser.Name", WebUser.Name);
                        sql = sql.Replace("@WebUser.FK_Dept", WebUser.DeptNo);
                        sql = this.DealExpByFromVals(sql);
                    }

                    if (cond.Contains("#EnumKey=") == true)
                    {
                        string enumKey = cond.Substring(cond.IndexOf("EnumKey") + 8);
                        sql = "SELECT IntKey AS No, Lab as Name FROM " + BP.Sys.Base.Glo.SysEnum() + " WHERE EnumKey='" + enumKey + "'";
                    }
                    if (sql == null)
                        continue;

                    //参数.
                    string para = cond.Substring(5, cond.IndexOf("#") - 5);
                    if (ds.Tables.Contains(para) == true)
                        throw new Exception("@配置的查询,参数名有冲突不能命名为:" + para);

                    //查询出来数据，就把他放入到dataset里面.
                    DataTable dtPara = DBAccess.RunSQLReturnTable(sql);
                    dtPara.TableName = para;
                    ds.Tables.Add(dtPara); //加入到参数集合.
                }


                return BP.Tools.Json.ToJson(ds);
            }
            //返回数据.
            return BP.Tools.Json.ToJson(ds);
        }

        /// <summary>
        /// /// <summary>
        /// 初始化PopVal分页表格的List  杨玉慧
        /// </summary>
        /// <returns></returns>
        public string PopVal_InitTablePageList()
        {
            MapExt me = new MapExt();
            me.setMyPK(this.FK_MapExt);
            me.Retrieve();

            //数据对象，将要返回的.
            DataSet ds = new DataSet();

            //获得配置信息.
            Hashtable ht = me.PopValToHashtable();
            DataTable dtcfg = BP.Pub.PubClass.HashtableToDataTable(ht);

            //增加到数据源.
            ds.Tables.Add(dtcfg);

            if (me.PopValWorkModel == PopValWorkModel.SelfUrl)
                return "@SelfUrl" + me.PopValUrl;
            if (me.PopValWorkModel == PopValWorkModel.TablePage)
            {
                /* 分页的 */
                //key
                string key = this.GetRequestVal("Key");
                if (DataType.IsNullOrEmpty(key) == true)
                    key = "";

                //取出来查询条件.
                string[] conds = me.PopValSearchCond.Split('$');

                //pageSize
                string pageSize = this.GetRequestVal("pageSize");
                if (DataType.IsNullOrEmpty(pageSize))
                    pageSize = "10";

                //pageIndex
                string pageIndex = this.GetRequestVal("pageIndex");
                if (DataType.IsNullOrEmpty(pageIndex))
                    pageIndex = "1";

                string sqlObjs = me.PopValTablePageSQL;
                sqlObjs = sqlObjs.Replace("@WebUser.No", BP.Web.WebUser.No);
                sqlObjs = sqlObjs.Replace("@WebUser.Name", BP.Web.WebUser.Name);
                sqlObjs = sqlObjs.Replace("@WebUser.FK_Dept", BP.Web.WebUser.DeptNo);
                sqlObjs = sqlObjs.Replace("@Key", key);

                //三个固定参数.
                sqlObjs = sqlObjs.Replace("@PageCount", ((int.Parse(pageIndex) - 1) * int.Parse(pageSize)).ToString());
                sqlObjs = sqlObjs.Replace("@PageSize", pageSize);
                sqlObjs = sqlObjs.Replace("@PageIndex", pageIndex);
                sqlObjs = this.DealExpByFromVals(sqlObjs);

                //替换其他参数.
                foreach (string cond in conds)
                {
                    if (cond == null || cond == "")
                        continue;

                    //参数.
                    string para = cond.Substring(5, cond.IndexOf("#") - 5);
                    string val = HttpContextHelper.RequestParams(para);
                    if (DataType.IsNullOrEmpty(val))
                    {
                        if (cond.Contains("ListSQL") == true || cond.Contains("EnumKey") == true)
                            val = "all";
                        else
                            val = "";
                    }
                    if (val == "all")
                    {
                        sqlObjs = sqlObjs.Replace(para + "=@" + para, "1=1");
                        sqlObjs = sqlObjs.Replace(para + "='@" + para + "'", "1=1");

                        int startIndex = 0;
                        while (startIndex != -1 && startIndex < sqlObjs.Length)
                        {
                            int index = sqlObjs.IndexOf("1=1", startIndex + 1);
                            if (index > 0 && sqlObjs.Substring(startIndex, index - startIndex).Trim().EndsWith("."))
                            {
                                int lastBlankIndex = sqlObjs.Substring(startIndex, index - startIndex).LastIndexOf(" ");


                                sqlObjs = sqlObjs.Remove(lastBlankIndex + startIndex + 1, index - lastBlankIndex - 1);

                                startIndex = (startIndex + lastBlankIndex) + 3;
                            }
                            else
                            {
                                startIndex = index;
                            }
                        }
                    }
                    else
                    {
                        //要执行两次替换有可能是，有引号.
                        sqlObjs = sqlObjs.Replace("@" + para, val);
                    }
                }


                DataTable dt = DBAccess.RunSQLReturnTable(sqlObjs);
                dt.TableName = "DTObjs";
                ds.Tables.Add(dt);

                //处理查询条件.
                //$Para=Dept#Label=所在班级#ListSQL=Select No,Name FROM Port_Dept WHERE No='@WebUser.No'
                //$Para=XB#Label=性别#EnumKey=XB
                //$Para=DTFrom#Label=注册日期从#DefVal=@Now-30
                //$Para=DTTo#Label=到#DefVal=@Now


                foreach (string cond in conds)
                {
                    if (DataType.IsNullOrEmpty(cond) == true)
                        continue;

                    string sql = null;
                    if (cond.Contains("#ListSQL=") == true)
                    {
                        sql = cond.Substring(cond.IndexOf("ListSQL") + 8);
                        sql = sql.Replace("@WebUser.No", WebUser.No);
                        sql = sql.Replace("@WebUser.Name", WebUser.Name);
                        sql = sql.Replace("@WebUser.FK_Dept", WebUser.DeptNo);
                        sql = this.DealExpByFromVals(sql);
                    }

                    if (cond.Contains("#EnumKey=") == true)
                    {
                        string enumKey = cond.Substring(cond.IndexOf("EnumKey") + 8);
                        sql = "SELECT IntKey AS No, Lab as Name FROM " + BP.Sys.Base.Glo.SysEnum() + " WHERE EnumKey='" + enumKey + "'";
                    }

                    //处理日期的默认值
                    //DefVal=@Now-30
                    //if (cond.Contains("@Now"))
                    //{
                    //    int nowIndex = cond.IndexOf(cond);
                    //    if (cond.Trim().Length - nowIndex > 5)
                    //    {
                    //        char optStr = cond.Trim()[nowIndex + 5];
                    //        int day = 0;
                    //        if (int.TryParse(cond.Trim().Substring(nowIndex + 6), out day)) {
                    //            cond = cond.Substring(0, nowIndex) + DateTime.Now.AddDays(-1 * day).ToString("yyyy-MM-dd HH:mm");
                    //        }
                    //    }
                    //}

                    if (sql == null)
                        continue;

                    //参数.
                    string para = cond.Substring(5, cond.IndexOf("#") - 5);
                    if (ds.Tables.Contains(para) == true)
                        throw new Exception("@配置的查询,参数名有冲突不能命名为:" + para);

                    //查询出来数据，就把他放入到dataset里面.
                    DataTable dtPara = DBAccess.RunSQLReturnTable(sql);
                    dtPara.TableName = para;
                    ds.Tables.Add(dtPara); //加入到参数集合.
                }


                return BP.Tools.Json.ToJson(ds);
            }
            //返回数据.
            return BP.Tools.Json.ToJson(ds);
        }

        //单附件上传方法
        private void SingleAttach(string attachPk, Int64 workid, Int64 fid, int fk_node, string ensName)
        {
            FrmAttachment frmAth = new FrmAttachment();
            frmAth.setMyPK(attachPk);
            frmAth.RetrieveFromDBSources();

            string athDBPK = attachPk + "_" + workid;

            BP.WF.Node currND = new BP.WF.Node(fk_node);
            BP.WF.Work currWK = currND.HisWork;
            currWK.OID = workid;
            currWK.Retrieve();
            //处理保存路径.
            string saveTo = frmAth.SaveTo;

            if (saveTo.Contains("*") || saveTo.Contains("@"))
            {
                /*如果路径里有变量.*/
                saveTo = saveTo.Replace("*", "@");
                saveTo = BP.WF.Glo.DealExp(saveTo, currWK, null);
            }

            try
            {
                saveTo = BP.Difference.SystemConfig.PathOfWebApp + saveTo; //context.Server.MapPath("~/" + saveTo);
            }
            catch
            {
                //saveTo = saveTo;
            }

            if (System.IO.Directory.Exists(saveTo) == false)
                System.IO.Directory.CreateDirectory(saveTo);


            saveTo = saveTo + "/" + athDBPK + "." + HttpContextHelper.RequestFiles(0).FileName.Substring(HttpContextHelper.RequestFiles(0).FileName.LastIndexOf('.') + 1);
            //context.Request.Files[0].SaveAs(saveTo);
            HttpContextHelper.UploadFile(HttpContextHelper.RequestFiles(0), saveTo);

            FileInfo info = new FileInfo(saveTo);

            FrmAttachmentDB dbUpload = new FrmAttachmentDB();
            dbUpload.setMyPK(athDBPK);
            dbUpload.FK_FrmAttachment = attachPk;
            dbUpload.RefPKVal = this.WorkID.ToString();
            dbUpload.FID = fid;
            dbUpload.FrmID = ensName;

            dbUpload.FileExts = info.Extension;

            #region 处理文件路径，如果是保存到数据库，就存储pk.
            if (frmAth.AthSaveWay == AthSaveWay.IISServer)
            {
                //文件方式保存
                dbUpload.FileFullName = saveTo;
            }

            if (frmAth.AthSaveWay == AthSaveWay.DB)
            {
                //保存到数据库
                dbUpload.FileFullName = dbUpload.MyPK;
            }
            #endregion 处理文件路径，如果是保存到数据库，就存储pk.


            dbUpload.FileName = HttpContextHelper.RequestFiles(0).FileName;
            dbUpload.FileSize = (float)info.Length;
            dbUpload.Rec = WebUser.No;
            dbUpload.RecName = WebUser.Name;
            dbUpload.DeptNo = WebUser.DeptNo;
            dbUpload.DeptName = WebUser.DeptName;
            dbUpload.RDT = DataType.CurrentDateTime;

            dbUpload.NodeID = fk_node;
            dbUpload.Save();

            if (frmAth.AthSaveWay == AthSaveWay.DB)
            {
                //执行文件保存.
                DBAccess.SaveFileToDB(saveTo, dbUpload.EnMap.PhysicsTable, "MyPK", dbUpload.MyPK, "FDB");
            }
        }

        //多附件上传方法
        public string MoreAttach()
        {
            string empNo = this.GetRequestVal("UserNo");
            if (string.IsNullOrEmpty(empNo) == false)
            {
                BP.WF.Dev2Interface.Port_Login(empNo);
            }

            string uploadFileM = ""; //上传附件数据的MyPK,用逗号分开
            List<string> UFPaths = new List<string>(); ;
            string pkVal = this.GetRequestVal("PKVal");
            string attachPk = this.GetRequestVal("AttachPK");
            string paras = this.GetRequestVal("parasData");
            string sort = this.GetRequestVal("Sort");

            float latitude = this.GetRequestValFloat("Latitude");
            float longitude = this.GetRequestValFloat("Longitude");
            float orientation = this.GetRequestValFloat("Orientation");
            // 多附件描述.
            BP.Sys.FrmAttachment athDesc = new BP.Sys.FrmAttachment(attachPk);
            MapData mapData = null;
            GEEntity en = null;
            string msg = "";
            if (athDesc.FrmID.StartsWith("TS.") == true)
            {
                pkVal = this.PKVal;
            }
            else
            {
                mapData = new MapData(athDesc.FrmID);

                //求出来实体记录，方便执行事件.
                en = new GEEntity(athDesc.FrmID);
                en.PKVal = pkVal;
                if (en.RetrieveFromDBSources() == 0)
                {
                    en.PKVal = this.FID;
                    if (en.RetrieveFromDBSources() == 0)
                    {
                        en.PKVal = this.PWorkID;
                        en.RetrieveFromDBSources();
                    }
                }

                //求主键. 如果该表单挂接到流程上.
                if (this.NodeID != 0 && !(athDesc.NoOfObj.Contains("AthMDtl") == true || athDesc.GetParaBoolen("IsDtlAth") == true))
                {
                    //判断表单方案。
                    FrmNode fn = new FrmNode(this.NodeID, this.FrmID);
                    if (fn.FrmSln == FrmSln.Self)
                    {
                        BP.Sys.FrmAttachment myathDesc = new FrmAttachment();
                        myathDesc.setMyPK(attachPk + "_" + this.NodeID);
                        if (myathDesc.RetrieveFromDBSources() != 0)
                            athDesc.HisCtrlWay = myathDesc.HisCtrlWay;
                    }
                    pkVal = BP.WF.Dev2Interface.GetAthRefPKVal(this.WorkID, this.PWorkID, this.FID, this.NodeID, this.FrmID, athDesc);
                }

            }
            for (int i = 0; i < HttpContextHelper.RequestFilesCount; i++)
            {
                IFormFile file = HttpContextHelper.RequestFiles(i);

                string guid = DBAccess.GenerGUID();
                string tempFilePath = BP.Difference.SystemConfig.PathOfTemp + guid + ".tmp";
                string fileName = System.IO.Path.GetFileName(file.FileName);
                string exts = System.IO.Path.GetExtension(file.FileName).ToLower().Replace(".", "");
                if (DataType.IsNullOrEmpty(exts))
                    return "err@上传的文件" + file.FileName + "没有扩展名";

                //保存到临时文件
                HttpContextHelper.UploadFile(file, tempFilePath);
                FrmAttachmentDB dbUpload = new FrmAttachmentDB();
                FileInfo info = new FileInfo(tempFilePath);
                dbUpload.MyPK = guid;
                dbUpload.RefPKVal = pkVal;
                dbUpload.FID = this.FID;
                dbUpload.Sort = sort;
                dbUpload.NodeID = this.NodeID;
                dbUpload.FrmID = athDesc.FrmID;
                dbUpload.FK_FrmAttachment = athDesc.MyPK;
                dbUpload.FileName = fileName;
                dbUpload.FileSize = (float)info.Length;
                dbUpload.FileExts = exts;
                dbUpload.RDT = DataType.CurrentDateTimess;
                dbUpload.Rec = BP.Web.WebUser.No;
                dbUpload.RecName = BP.Web.WebUser.Name;
                dbUpload.DeptNo = WebUser.DeptNo;
                dbUpload.DeptName = WebUser.DeptName;
                dbUpload.SetPara("IsEncrypt", BP.Difference.SystemConfig.isEnableAthEncrypt ? 1 : 0);
                dbUpload.SetPara("Latitude", latitude);
                dbUpload.SetPara("Longitude", longitude);
                dbUpload.SetPara("Orientation", orientation);

                dbUpload.GenerFileUpload(tempFilePath, athDesc, false);
                uploadFileM += guid + ",";
                UFPaths.Add(dbUpload.FileFullName);
            }
            //需要判断是否存在AthNum字段 
            if (en != null && en.Row["AthNum"] != null)
            {
                int athNum = int.Parse(en.Row["AthNum"].ToString());
                en.Row["AthNum"] = athNum + 1;
                en.Update();
            }
            //return uploadFileM;
            if (string.IsNullOrEmpty(empNo))
                return uploadFileM;
            else
            {
                Log.DebugWriteInfo("上传成功");
                var jsonObject = new JObject();
                jsonObject["msg"] = "上传成功";
                jsonObject["MyPKs"] = uploadFileM;
                jsonObject["FileFullName"] = string.Join(",", UFPaths.Where(p => p != null));
                return jsonObject.ToString();
            }
        }

        /// <summary>
        /// 执行附件地址添加KKFile队列预加载
        /// </summary>
        /// <returns></returns>
        public string DoKKFileAddTask()
        {
            String url = this.GetRequestVal("URL");
            String response = BP.Tools.HttpUtil.doGetStr(url, null, null, "application/json");
            return response;
        }

        /// <summary>
        /// 删除附件
        /// </summary>
        /// <returns></returns>
        public string DelWorkCheckAttach()
        {
            FrmAttachmentDB athDB = new FrmAttachmentDB();
            athDB.RetrieveByAttr(FrmAttachmentDBAttr.MyPK, this.MyPK);

            //删除文件
            if (athDB.FileFullName != null)
            {
                if (File.Exists(athDB.FileFullName) == true)
                    File.Delete(athDB.FileFullName);
            }

            int i = athDB.Delete(FrmAttachmentDBAttr.MyPK, this.MyPK);
            if (i > 0)
                return "true";
            return "false";
        }

        public string FrmVSTO_Init()
        {
            return "";
        }
        /// <summary>
        /// 表单处理加载
        /// </summary>
        /// <returns></returns>
        public string FrmSingle_Init()
        {
            if (DataType.IsNullOrEmpty(this.FrmID))
                throw new Exception("FK_MapData参数不能为空");

            MapData md = new MapData();
            md.No = this.FrmID;

            if (md.RetrieveFromDBSources() == 0)
                throw new Exception("未检索到FK_MapData=" + this.FrmID + "的表单，请核对参数");

            int minOID = 10000000;//最小OID设置为一千万
            int oid = this.OID;
            Hashtable ht = new Hashtable();
            GEEntityOID en = md.HisGEEn;

            if (oid == 0)
                oid = minOID;

            en.OID = oid;

            if (en.RetrieveFromDBSources() == 0)
            {
                ht.Add("IsExist", 0);
            }
            else
            {
                ht.Add("IsExist", 1);
            }

            ht.Add("OID", oid);
            ht.Add("UserNo", WebUser.No);
            ht.Add("Token", WebUser.Token);

            return BP.Tools.Json.ToJsonEntityModel(ht);
        }

        #region 从表的选项.
        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <returns></returns>
        public string DtlOpt_Init()
        {
            MapDtl dtl = new MapDtl(this.MapDtlNo);

            if (dtl.ImpModel == 0)
                return "err@该从表不允许导入.";

            if (dtl.ImpModel == 2)
                return "url@DtlImpByExcel.htm?FK_MapDtl=" + this.MapDtlNo;


            if (DataType.IsNullOrEmpty(dtl.ImpSQLInit))
                return "err@从表加载语句为空，请设置从表加载的sql语句。";

            DataSet ds = new DataSet();
            DataTable dt = DBAccess.RunSQLReturnTable(dtl.ImpSQLInit);

            return BP.Tools.Json.ToJson(dt);

        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <returns></returns>
        public string DtlOpt_Add()
        {
            MapDtl dtl = new MapDtl(this.MapDtlNo);
            string pks = this.GetRequestVal("PKs");

            string[] strs = pks.Split(',');
            int i = 0;
            foreach (string str in strs)
            {
                if (DataType.IsNullOrEmpty(str) == true || str.Equals("CheckAll") == true)
                    continue;

                GEDtl gedtl = new BP.Sys.GEDtl(this.MapDtlNo);
                string sql = dtl.ImpSQLFullOneRow;
                sql = sql.Replace("@Key", str);

                DataTable dt = DBAccess.RunSQLReturnTable(sql);

                if (dt.Rows.Count == 0)
                    return "err@导入数据失败:" + sql;

                gedtl.Copy(dt.Rows[0]);
                gedtl.RefPK = this.GetRequestVal("RefPKVal");
                gedtl.OID = 0;
                gedtl.InsertAsOID(DBAccess.GenerOID(dtl.PTable));
                i++;
            }

            return "成功的导入了[" + i + "]行数据...";
        }
        /// <summary>
        /// 执行查询.
        /// </summary>
        /// <returns></returns>
        public string DtlOpt_Search()
        {
            MapDtl dtl = new MapDtl(this.MapDtlNo);

            string sql = dtl.ImpSQLSearch;
            sql = sql.Replace("@Key", this.GetRequestVal("Key"));
            sql = sql.Replace("@WebUser.No", WebUser.No);
            sql = sql.Replace("@WebUser.Name", WebUser.Name);
            sql = sql.Replace("@WebUser.FK_Dept", WebUser.DeptNo);

            DataSet ds = new DataSet();
            DataTable dt = DBAccess.RunSQLReturnTable(sql);
            return BP.Tools.Json.ToJson(dt);
        }
        #endregion 从表的选项.

        #region SQL从表导入.
        public string DtlImpBySQL_Delete()
        {
            MapDtl dtl = new MapDtl(this.EnsName);
            DBAccess.RunSQL("DELETE FROM " + dtl.PTable + " WHERE RefPK='" + this.RefPKVal + "'");
            return "";
        }
        /// <summary>
        /// SQL从表导入
        /// </summary>
        /// <returns></returns>
        public string DtlImpBySQl_Imp()
        {
            //获取参数
            string ensName = this.EnsName;
            string refpk = this.RefPKVal;
            long pworkID = this.PWorkID;
            int fkNode = this.NodeID;
            long fid = this.FID;
            string pk = this.GetRequestVal("PKs");
            GEDtls dtls = new GEDtls(ensName);
            QueryObject qo = new QueryObject(dtls);
            //获取从表权限
            MapDtl dtl = new MapDtl(ensName);
            #region 处理权限方案。
            if (this.NodeID != 0 && this.NodeID != 999999)
            {
                Node nd = new Node(this.NodeID);
                if (nd.HisFormType == NodeFormType.SheetTree || nd.HisFormType == NodeFormType.RefOneFrmTree)
                {
                    FrmNode fn = new FrmNode(nd.NodeID, dtl.FrmID);
                    if (fn.FrmSln == FrmSln.Self)
                    {
                        string no = this.MapDtlNo + "_" + nd.NodeID;
                        MapDtl mdtlSln = new MapDtl();
                        mdtlSln.No = no;
                        int result = mdtlSln.RetrieveFromDBSources();
                        if (result != 0)
                        {
                            dtl = mdtlSln;

                        }
                    }
                }
            }
            #endregion 处理权限方案。

            //判断是否重复导入
            bool isInsert = true;
            if (DataType.IsNullOrEmpty(pk) == false)
            {
                string[] pks = pk.Split('@');
                int idx = 0;
                foreach (string k in pks)
                {
                    if (DataType.IsNullOrEmpty(k))
                        continue;
                    if (idx == 0)
                        qo.AddWhere(k, this.GetRequestVal(k));
                    else
                    {
                        qo.addAnd();
                        qo.AddWhere(k, this.GetRequestVal(k));
                    }
                    idx++;
                }
                switch (dtl.DtlOpenType)
                {
                    case DtlOpenType.ForEmp:  // 按人员来控制.
                        qo.addAnd();
                        qo.AddWhere("RefPk", refpk);
                        qo.addAnd();
                        qo.AddWhere("Rec", this.GetRequestVal("UserNo"));
                        break;
                    case DtlOpenType.ForWorkID: // 按工作ID来控制
                        qo.addAnd();
                        qo.addLeftBracket();
                        qo.AddWhere("RefPk", refpk);
                        qo.addOr();
                        qo.AddWhere("FID", fid);
                        qo.addRightBracket();
                        break;
                    case DtlOpenType.ForFID: // 按流程ID来控制.
                        qo.addAnd();
                        qo.AddWhere("FID", fid);
                        break;
                }

                int count = qo.GetCount();
                if (count > 0)
                    isInsert = false;
            }
            //导入数据
            if (isInsert == true)
            {

                GEDtl dtlEn = dtls.GetNewEntity as GEDtl;
                //遍历属性，循环赋值.
                foreach (Attr attr in dtlEn.EnMap.Attrs)
                {
                    dtlEn.SetValByKey(attr.Key, this.GetRequestVal(attr.Key));

                }
                switch (dtl.DtlOpenType)
                {
                    case DtlOpenType.ForEmp:  // 按人员来控制.
                        dtlEn.RefPKInt = int.Parse(refpk);
                        break;
                    case DtlOpenType.ForWorkID: // 按工作ID来控制
                        dtlEn.RefPKInt = int.Parse(refpk);
                        dtlEn.SetValByKey("FID", int.Parse(refpk));
                        break;
                    case DtlOpenType.ForFID: // 按流程ID来控制.
                        dtlEn.RefPKInt = int.Parse(refpk);
                        dtlEn.SetValByKey("FID", fid);
                        break;
                }
                dtlEn.SetValByKey("RDT", DataType.CurrentDate);
                dtlEn.SetValByKey("Rec", this.GetRequestVal("UserNo"));
                //dtlEn.OID = (int)DBAccess.GenerOID(ensName);

                dtlEn.InsertAsOID(DBAccess.GenerOID(dtl.PTable));
                return dtlEn.OID.ToString();
            }

            return "";

        }
        #endregion SQL从表导入

        #region Excel导入.
        /// <summary>
        /// 获得sheetNames
        /// </summary>
        /// <returns></returns>
        public string ImpExcel_GetSheets()
        {
            string file = "";
            string[] strs = DBLoad.GenerTableNames(file);
            return strs.ToString();
        }
        public string ImpExcel_Done()
        {
            return "";
        }
        /// <summary>
        /// 导入excel.
        /// </summary>
        /// <returns></returns>
        public string DtlImpByExcel_Imp()
        {
            try
            {
                string tempPath = BP.Difference.SystemConfig.PathOfTemp;

                //HttpFileCollection files = context.Request.Files;
                IFormFileCollection files = HttpContextHelper.RequestFiles();
                if (files.Count == 0)
                    return "err@请选择要上传的从表模版。";
                //求出扩展名.
                string fileName = files[0].FileName;
                if (fileName.Contains(".xls") == false)
                {
                    return "err@上传的文件必须是excel文件.";
                }

                string ext = ".xls";
                if (fileName.Contains(".xlsx"))
                    ext = ".xlsx";

                //保存临时文件.
                string file = tempPath + "/" + WebUser.No + ext;

                if (System.IO.Directory.Exists(tempPath) == false)
                    System.IO.Directory.CreateDirectory(tempPath);

                //执行保存附件
                //files[0].SaveAs(file);
                HttpContextHelper.UploadFile(files[0], file);

                DataTable dt = DBLoad.ReadExcelFileToDataTable(file);

                string FK_MapDtl = this.MapDtlNo;
                if (FK_MapDtl.Contains("BP") == true)
                {
                    return BPDtlImpByExcel_Imp(dt, FK_MapDtl);
                }

                MapDtl dtl = new MapDtl(this.MapDtlNo);

                #region 处理权限方案。
                if (this.NodeID != 0 && this.NodeID != 999999)
                {
                    Node nd = new Node(this.NodeID);
                    if (nd.HisFormType == NodeFormType.SheetTree || nd.HisFormType == NodeFormType.RefOneFrmTree)
                    {
                        FrmNode fn = new FrmNode(nd.NodeID, this.FrmID);
                        if (fn.FrmSln == FrmSln.Self)
                        {
                            string no = this.MapDtlNo + "_" + nd.NodeID;
                            MapDtl mdtlSln = new MapDtl();
                            mdtlSln.No = no;
                            int result = mdtlSln.RetrieveFromDBSources();
                            if (result != 0)
                            {
                                dtl = mdtlSln;
                                //fk_mapDtl = no;
                            }
                        }
                    }
                }
                #endregion 处理权限方案。

                GEDtls dtls = new GEDtls(this.MapDtlNo);

                #region 检查两个文件是否一致。 生成要导入的属性
                BP.En.Attrs attrs = dtls.GetNewEntity.EnMap.Attrs;
                BP.En.Attrs attrsExp = new BP.En.Attrs();

                bool isHave = false;
                foreach (DataColumn dc in dt.Columns)
                {
                    foreach (BP.En.Attr attr in attrs)
                    {
                        if (dc.ColumnName == attr.Desc)
                        {
                            isHave = true;
                            attrsExp.Add(attr);
                            continue;
                        }

                        if (dc.ColumnName.ToLower() == attr.Key.ToLower())
                        {
                            isHave = true;
                            attrsExp.Add(attr);
                            dc.ColumnName = attr.Desc;
                        }
                    }
                }
                if (isHave == false)
                    throw new Exception("@您导入的excel文件不符合系统要求的格式，请下载模版文件重新填入。");
                #endregion

                #region 执行导入数据.

                //清空模式导入.
                string refPK = this.GetRequestVal("WorkID");
                if (this.GetRequestValInt("DDL_ImpWay") == 0)
                    DBAccess.RunSQL("DELETE FROM " + dtl.PTable + " WHERE RefPK='" + refPK + "'");

                int i = 0;
                Int64 oid = DBAccess.GenerOID("Dtl", dt.Rows.Count);
                string rdt = DataType.CurrentDate;

                //获取枚举
                SysEnums enums = new SysEnums();
                enums.RetrieveInSQL(SysEnumAttr.EnumKey, "SELECT UIBindKey From Sys_MapAttr WHERE FK_MapData='" + dtl.No + "' AND LGType=1");
                DataTable dtt = null;
                //获取外键、外部数据源
                DataSet ds = new DataSet();
                string treeKey = dtl.InitDBAttrs;
                Attr attrKey = null;
                foreach (BP.En.Attr attr in attrs)
                {
                    if (DataType.IsNullOrEmpty(attr.UIBindKey) == true)
                        continue;
                    if (attr.MyFieldType == FieldType.Enum || attr.MyFieldType == FieldType.PKEnum)
                        continue;
                    if (ds.Tables.Contains(attr.UIBindKey) == true)
                        continue;
                    dtt = BP.Pub.PubClass.GetDataTableByUIBineKey(attr.UIBindKey);

                    dtt.TableName = attr.UIBindKey;
                    ds.Tables.Add(dtt);
                    if (DataType.IsNullOrEmpty(treeKey) == false && attr.Key.Equals(treeKey))
                        attrKey = attr;
                }

                string errMsg = "";
                string treeNames = ",";

                int errIdx = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    GEDtl dtlEn = dtls.GetNewEntity as GEDtl;
                    dtlEn.ResetDefaultVal();
                    string rowMsg = "";
                    foreach (BP.En.Attr attr in attrsExp)
                    {
                        if (attr.UIVisible == false || dr[attr.Desc] == DBNull.Value)
                            continue;
                        string val = dr[attr.Desc].ToString();
                        if (val == null)
                            continue;
                        val = val.Trim();
                        if (attrKey != null && attr.Key.Equals(treeKey))
                            treeNames += val + ",";
                        switch (attr.MyFieldType)
                        {
                            case FieldType.Enum:
                            case FieldType.PKEnum:
                                SysEnums ses = enums.GetEntitiesByKey(SysEnumAttr.EnumKey, attr.UIBindKey) as SysEnums;
                                bool isHavel = false;
                                foreach (SysEnum se in ses)
                                {
                                    if (val.Equals(se.Lab))
                                    {
                                        if (DataType.IsNullOrEmpty(se.StrKey) == false)
                                            val = se.StrKey;
                                        else
                                            val = se.IntKey.ToString();
                                        isHavel = true;
                                        break;
                                    }
                                }
                                if (isHavel == false)
                                {
                                    rowMsg += "@数据格式不规范,第(" + i + ")行，列(" + attr.Desc + ")，数据(" + val + ")不符合格式,改值没有在枚举列表里.";
                                    val = attr.DefaultVal.ToString();
                                }
                                break;
                            case FieldType.FK:
                            case FieldType.PKFK:
                                dtt = ds.Tables[attr.UIBindKey];
                                bool isHavelIt = false;
                                foreach (DataRow drr in dtt.Rows)
                                {
                                    if (val.Equals(drr["Name"].ToString()))
                                    {
                                        val = drr["No"].ToString();
                                        isHavelIt = true;
                                        break;
                                    }
                                }
                                if (isHavelIt == false)
                                    rowMsg += "@数据格式不规范,第(" + i + ")行，列(" + attr.Desc + ")，数据(" + val + ")不符合格式,改值没有在外键数据列表里.";
                                break;
                            default:
                                if (attr.UIContralType == UIContralType.DDL)
                                {
                                    dtt = ds.Tables[attr.UIBindKey];
                                    bool isHaveIt = false;
                                    foreach (DataRow drr in dtt.Rows)
                                    {
                                        if (val.Equals(drr["Name"].ToString()))
                                        {
                                            val = drr["No"].ToString();
                                            isHaveIt = true;
                                            dtlEn.SetValByKey(attr.Key + "T", drr["Name"].ToString());
                                            break;
                                        }
                                    }
                                    if (isHaveIt == false)
                                        rowMsg += "@数据格式不规范,第(" + i + ")行，列(" + attr.Desc + ")，数据(" + val + ")不符合格式,改值没有在外键数据列表里.";
                                    break;
                                }
                                break;
                        }

                        if (attr.MyDataType == DataType.AppBoolean)
                        {
                            if (val.Trim().Equals("是") == true || val.Trim().ToLower().Equals("yes") == true)
                                val = "1";

                            if (val.Trim().Equals("否") == true || val.Trim().ToLower().Equals("no") == true)
                                val = "0";
                        }

                        dtlEn.SetValByKey(attr.Key, val);
                    }
                    if (DataType.IsNullOrEmpty(rowMsg) == false)
                    {
                        errMsg += rowMsg;
                        errIdx++;
                        continue;
                    }
                    //dtlEn.RefPKInt = (int)this.WorkID;
                    //关联主赋值.
                    dtlEn.RefPK = this.RefPKVal;
                    switch (dtl.DtlOpenType)
                    {
                        case DtlOpenType.ForEmp:  // 按人员来控制.
                            dtlEn.RefPK = refPK;
                            break;
                        case DtlOpenType.ForWorkID: // 按工作ID来控制
                            dtlEn.RefPK = refPK;
                            dtl.SetValByKey("FID", refPK);
                            break;
                        case DtlOpenType.ForFID: // 按流程ID来控制.
                            dtlEn.RefPK = refPK;
                            dtl.SetValByKey("FID", this.FID);
                            break;
                    }
                    dtlEn.SetValByKey("RDT", rdt);
                    dtlEn.SetValByKey("Rec", WebUser.No);
                    dtlEn.SetValByKey("Idx", i);
                    i++;
                    //dtlEn.OID = (int)DBAccess.GenerOID(this.EnsName);
                    dtlEn.InsertAsOID(DBAccess.GenerOID(dtl.PTable));
                    oid++;
                }
                #endregion 执行导入数据.
                //如果从表是损溢表展示，需要把其他的数据插入
                if (dtl.GetValIntByKey(MapDtlAttr.ListShowModel) == 6 && DataType.IsNullOrEmpty(treeKey) == false)
                {
                    dtt = ds.Tables[attrKey.UIBindKey];
                    foreach (DataRow dr in dtt.Rows)
                    {
                        if (treeNames.Contains("," + dr[1].ToString() + ","))
                            continue;
                        GEDtl dtlEn = dtls.GetNewEntity as GEDtl;
                        dtlEn.ResetDefaultVal();
                        dtlEn.SetValByKey(attrKey.Key, dr[0]);
                        if (attrs.Contains(attrKey.Key + "T"))
                            dtlEn.SetValByKey(attrKey.Key + "T", dr[1]);
                        dtlEn.RefPK = this.RefPKVal;
                        switch (dtl.DtlOpenType)
                        {
                            case DtlOpenType.ForEmp:  // 按人员来控制.
                                dtlEn.RefPK = refPK;
                                break;
                            case DtlOpenType.ForWorkID: // 按工作ID来控制
                                dtlEn.RefPK = refPK;
                                dtlEn.SetValByKey("FID", refPK);
                                break;
                            case DtlOpenType.ForFID: // 按流程ID来控制.
                                dtlEn.RefPK = refPK;
                                dtlEn.SetValByKey("FID", this.FID);
                                break;
                        }
                        dtlEn.SetValByKey("RDT", rdt);
                        dtlEn.SetValByKey("Rec", WebUser.No);
                        dtlEn.InsertAsOID(DBAccess.GenerOID(dtl.PTable));
                    }
                }

                if (string.IsNullOrEmpty(errMsg) == true)
                    return "info@共有(" + i + ")条数据导入成功。";
                else
                    return "共有(" + errIdx + ")条数据导入成功，但是出现如下错误:" + errMsg;
            }
            catch (Exception ex)
            {
                string msg = ex.Message.Replace("'", "‘");
                return "err@" + msg;
            }
        }

        private static SemaphoreSlim _semaphore = new SemaphoreSlim(100); //限制最大并发数为100

        public string dtlImp_Save()
        {
            MapDtl mdtl = new MapDtl(this.EnsName);
            string dtlRefPKVal = this.RefPKVal;
            string fk_mapDtl = this.MapDtlNo;
            GEEntity mainEn = null;
            //获取集合
            string str = this.GetRequestVal("Json");
            if (DataType.IsNullOrEmpty(str) == true)
            {
                return "不需要保存";
            }

            ///#region 处理权限方案。
            if (this.NodeID != 0 && this.NodeID != 999999)
            {
                Node nd = new Node(this.NodeID);
                if (nd.HisFormType == NodeFormType.SheetTree || nd.HisFormType == NodeFormType.RefOneFrmTree)
                {
                    FrmNode fn = new FrmNode(nd.NodeID, this.FrmID);
                    if (fn.FrmSln == FrmSln.Self)
                    {
                        string no = fk_mapDtl + "_" + nd.NodeID;
                        MapDtl mdtlSln = new MapDtl();
                        mdtlSln.No = no;
                        int result = mdtlSln.RetrieveFromDBSources();
                        if (result != 0)
                        {
                            mdtl = mdtlSln;
                            fk_mapDtl = no;
                        }
                    }
                }
                dtlRefPKVal = Dev2Interface.GetDtlRefPKVal(this.WorkID, this.PWorkID, this.FID, this.NodeID, this.FrmID, mdtl);
                if (dtlRefPKVal.Equals("0") == true)
                {
                    dtlRefPKVal = this.RefPKVal;
                }
            }
            ///#endregion 处理权限方案。
            JArray json = JArray.Parse(str);
            int batchSize = 30;
            if (json.Count > 30)
            {
                int totalBatches = (json.Count % batchSize) == 0 ? (json.Count / batchSize) : (json.Count / batchSize) + 1;
                //需要分页
                long oid = -1;
                while (oid < 0)
                {
                    oid = DBAccess.GenerOID(mdtl.PTable, json.Count);
                }
                GEEntityOIDs dtls = new GEEntityOIDs(this.EnsName);
                long fid = this.FID;
                CountdownEvent cdEvent = new CountdownEvent(totalBatches);
                for (int i = 0; i < totalBatches; i++)
                {
                    int start = i * batchSize;
                    int end = start + batchSize;
                    if (i == totalBatches - 1)
                        end = json.Count;
                    long startOID = i == 0 ? oid : oid + i * batchSize;
                    string refPK = dtlRefPKVal;
                    string userNo = WebUser.No;
                    JArray drrs = null;
                    if (i != 0)
                        drrs = new JArray(json.Skip(start).Take(batchSize));
                    else
                        drrs = new JArray(json.Take(end));
                    ThreadPool.QueueUserWorkItem(o =>
                    {
                        _semaphore.Wait();
                        try
                        {
                            dtlImp_OneRow_Insert(dtls, drrs, refPK, startOID, fid, userNo);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("err@从表表格导入报错:" + ex.Message);
                        }
                        finally
                        {
                            _semaphore.Release();
                            cdEvent.Signal();
                        }
                    });
                }
                cdEvent.Wait();
            }
            return "导入成功";
        }
        private string dtlImp_OneRow_Insert(GEEntityOIDs dtls, JArray drrs, String dtlRefPKVal, long oid, long fid, String userNo)
        {
            foreach (JObject item in drrs)
            {
                oid = oid + 1;
                GEEntityOID dtl = dtls.GetNewEntity as GEEntityOID;
                if (dtl == null)
                    continue;
                foreach (var property in item.Properties())
                {
                    if (property == null)
                        continue;
                    dtl.SetValByKey(property.Name, property.Value);
                }
                dtl.SetValByKey("RefPK", dtlRefPKVal);
                if (fid == 0 && DataType.IsNumStr(dtlRefPKVal))
                {
                    dtl.SetValByKey("FID", Int64.Parse(dtlRefPKVal));
                }
                else
                {
                    dtl.SetValByKey("FID", fid);
                }
                dtl.SetValByKey("Rec", userNo);
                dtl.SetValByKey("RDT", DataType.CurrentDateTime);
                dtl.SetValByKey("OID", oid);
                dtl.InsertAsOID(oid);
            }
            return "更新成功";
        }
        /// <summary>
        /// BP类从表导入
        /// </summary>
        /// <returns></returns>
        private string BPDtlImpByExcel_Imp(DataTable dt, string fk_mapdtl)
        {
            try
            {
                #region 检查两个文件是否一致。 生成要导入的属性
                Entities dtls = ClassFactory.GetEns(this.MapDtlNo);
                EntityOID dtlEn = dtls.GetNewEntity as EntityOID;
                BP.En.Attrs attrs = dtlEn.EnMap.Attrs;
                BP.En.Attrs attrsExp = new BP.En.Attrs();

                bool isHave = false;
                foreach (DataColumn dc in dt.Columns)
                {
                    foreach (BP.En.Attr attr in attrs)
                    {
                        if (dc.ColumnName == attr.Desc)
                        {
                            isHave = true;
                            attrsExp.Add(attr);
                            continue;
                        }

                        if (dc.ColumnName.ToLower() == attr.Key.ToLower())
                        {
                            isHave = true;
                            attrsExp.Add(attr);
                            dc.ColumnName = attr.Desc;
                        }
                    }
                }
                if (isHave == false)
                    return "err@您导入的excel文件不符合系统要求的格式，请下载模版文件重新填入。";
                #endregion

                #region 执行导入数据.

                if (this.GetRequestValInt("DDL_ImpWay") == 0)
                    DBAccess.RunSQL("DELETE FROM " + dtlEn.EnMap.PhysicsTable + " WHERE RefPK='" + this.GetRequestVal("RefPKVal") + "'");

                int i = 0;
                Int64 oid = DBAccess.GenerOID(dtlEn.EnMap.PhysicsTable, dt.Rows.Count);
                string rdt = DataType.CurrentDate;

                string errMsg = "";
                foreach (DataRow dr in dt.Rows)
                {
                    dtlEn = dtls.GetNewEntity as EntityOID;
                    dtlEn.ResetDefaultVal();

                    foreach (BP.En.Attr attr in attrsExp)
                    {
                        if (attr.UIVisible == false || dr[attr.Desc] == DBNull.Value)
                            continue;
                        string val = dr[attr.Desc].ToString();
                        if (val == null)
                            continue;
                        val = val.Trim();
                        switch (attr.MyFieldType)
                        {
                            case FieldType.Enum:
                            case FieldType.PKEnum:
                                SysEnums ses = new SysEnums(attr.UIBindKey);
                                bool isHavel = false;
                                foreach (SysEnum se in ses)
                                {
                                    if (val == se.Lab)
                                    {
                                        val = se.IntKey.ToString();
                                        isHavel = true;
                                        break;
                                    }
                                }
                                if (isHavel == false)
                                {
                                    errMsg += "@数据格式不规范,第(" + i + ")行，列(" + attr.Desc + ")，数据(" + val + ")不符合格式,改值没有在枚举列表里.";
                                    val = attr.DefaultVal.ToString();
                                }
                                break;
                            case FieldType.FK:
                            case FieldType.PKFK:
                                Entities ens = null;
                                if (attr.UIBindKey.Contains("."))
                                    ens = BP.En.ClassFactory.GetEns(attr.UIBindKey);
                                else
                                    ens = new GENoNames(attr.UIBindKey, "desc");

                                ens.RetrieveAll();
                                bool isHavelIt = false;
                                foreach (Entity en in ens)
                                {
                                    if (val == en.GetValStrByKey("Name"))
                                    {
                                        val = en.GetValStrByKey("No");
                                        isHavelIt = true;
                                        break;
                                    }
                                }
                                if (isHavelIt == false)
                                    errMsg += "@数据格式不规范,第(" + i + ")行，列(" + attr.Desc + ")，数据(" + val + ")不符合格式,改值没有在外键数据列表里.";
                                break;
                            default:
                                break;
                        }

                        if (attr.MyDataType == DataType.AppBoolean)
                        {
                            if (val.Trim() == "是" || val.Trim().ToLower() == "yes")
                                val = "1";

                            if (val.Trim() == "否" || val.Trim().ToLower() == "no")
                                val = "0";
                        }

                        dtlEn.SetValByKey(attr.Key, val);
                    }

                    dtlEn.SetValByKey("RefPK", this.GetRequestVal("RefPKVal"));
                    i++;

                    dtlEn.Insert();
                }
                #endregion 执行导入数据.

                if (string.IsNullOrEmpty(errMsg) == true)
                    return "info@共有(" + i + ")条数据导入成功。";
                else
                    return "共有(" + i + ")条数据导入成功，但是出现如下错误:" + errMsg;

            }
            catch (Exception ex)
            {
                string msg = ex.Message.Replace("'", "‘");
                return "err@" + msg;
            }
        }
        #endregion  Excel导入.

        #region 打印.
        public string Print_Init()
        {
            //string ApplicationPath = this.context.Request.PhysicalApplicationPath;
            string ApplicationPath = HttpContextHelper.RequestApplicationPath;

            BP.WF.Node nd = new BP.WF.Node(this.NodeID);
            string path = ApplicationPath + @"DataUser/CyclostyleFile/FlowFrm/" + nd.FlowNo + "/" + nd.NodeID + "/";
            string[] fls = null;
            try
            {
                fls = System.IO.Directory.GetFiles(path);
            }
            catch (Exception ex)
            {
                return "err@" + ex.Message;
            }


            DataTable dt = new DataTable();
            dt.Columns.Add("BillNo");
            dt.Columns.Add("BillName");

            int idx = 0;
            int fileIdx = -1;
            foreach (string f in fls)
            {
                fileIdx++;
                string myfile = f.Replace(path, "");
                string[] strs = myfile.Split('.');
                idx++;

                DataRow dr = dt.NewRow();
                dr["BillNo"] = strs[0];
                dr["BillName"] = strs[1];

                dt.Rows.Add(dr);
            }
            //返回json.
            return BP.Tools.Json.ToJson(dt);
        }
        #endregion 打印.

        #region 附件组件.
        /// <summary>
        /// 执行删除
        /// </summary>
        /// <returns></returns>
        public string AttachmentUpload_Del()
        {
            //执行删除.
            string delPK = this.GetRequestVal("DelPKVal");

            FrmAttachmentDB delDB = new FrmAttachmentDB();
            delDB.setMyPK(delPK == null ? this.MyPK : delPK);
            delDB.RetrieveFromDBSources();
            delDB.Delete(); //删除上传的文件.
            return "删除成功.";
        }
        public string AttachmentUpload_DownByStream()
        {
            // return AttachmentUpload_Down(true);
            return AttachmentUpload_Down();
        }
        /// <summary>
        /// 下载
        /// </summary>
        /// <returns></returns>
        public string AttachmentUpload_Down()
        {
            //获取文件是否加密
            bool fileEncrypt = BP.Difference.SystemConfig.isEnableAthEncrypt;
            FrmAttachmentDB downDB = new FrmAttachmentDB();

            downDB.MyPK = this.MyPK;
            downDB.Retrieve();
            FrmAttachment dbAtt = new FrmAttachment();
            dbAtt.MyPK = downDB.FK_FrmAttachment;
            dbAtt.Retrieve();

            if (dbAtt.ReadRole != 0 && this.NodeID != 0)
            {
                //标记已经阅读了.
                GenerWorkerList gwf = new GenerWorkerList();
                int count = gwf.Retrieve(GenerWorkerListAttr.FK_Emp, BP.Web.WebUser.No,
                    GenerWorkerListAttr.FK_Node, this.NodeID, GenerWorkerListAttr.WorkID, this.WorkID);
                if (count != 0)
                {
                    string str = gwf.GetParaString(dbAtt.NoOfObj);
                    str += "," + downDB.MyPK;
                    gwf.SetPara(dbAtt.NoOfObj, str);
                    gwf.Update();
                }
            }
            string tempPath = downDB.GenerTempFile(dbAtt.AthSaveWay);
            bool isEncrypt = downDB.GetParaBoolen("IsEncrypt");
            string filepath = tempPath;

            if (fileEncrypt == true && isEncrypt == true)
            {
                filepath = tempPath + ".temp";
                EncHelper.DecryptDES(tempPath, filepath);
            }
            BP.WF.HttpHandler.HttpHandlerGlo.DownloadFile(filepath, downDB.FileName);
            return DataType.PraseStringToUrlFileName(filepath);
        }



        public void AttachmentDownFromByte()
        {
            FrmAttachmentDB downDB = new FrmAttachmentDB();
            downDB.setMyPK(this.MyPK);
            downDB.Retrieve();
            downDB.FileName = HttpUtility.UrlEncode(downDB.FileName);
            byte[] byteList = downDB.GetFileFromDB("DBFile", null);
            if (byteList != null)
            {
                //HttpContext.Current.Response.Charset = "GB2312";
                //HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + downDB.FileName);
                //HttpContext.Current.Response.ContentType = "application/octet-stream;charset=gb2312";
                //HttpContext.Current.Response.BinaryWrite(byteList);
                //HttpContext.Current.Response.End();
                //HttpContext.Current.Response.Close();
                HttpContextHelper.ResponseWriteFile(byteList, downDB.FileName, "application/octet-stream;charset=gb2312");
            }
        }
        /// <summary>
        /// 附件ID.
        /// </summary>
        public string FK_FrmAttachment
        {
            get
            {
                return this.GetRequestVal("FK_FrmAttachment");
            }
        }
        public BP.Sys.FrmAttachment GenerAthDescOfFoolTruck()
        {
            FoolTruckNodeFrm sln = new FoolTruckNodeFrm();
            sln.FrmSln = -1;
            string fromFrm = this.GetRequestVal("FromFrm");
            sln.setMyPK(fromFrm + "_" + this.NodeID + "_" + this.FlowNo);
            int result = sln.RetrieveFromDBSources();
            BP.Sys.FrmAttachment athDesc = new BP.Sys.FrmAttachment();
            athDesc.setMyPK(this.FK_FrmAttachment);
            athDesc.RetrieveFromDBSources();

            /*没有查询到解决方案, 就是只读方案 */
            if (result == 0 || sln.FrmSln == 1)
            {
                athDesc.ItIsUpload = false;
                athDesc.ItIsDownload = true;
                athDesc.HisDeleteWay = AthDeleteWay.None; //删除模式.
                return athDesc;
            }
            //默认方案
            if (sln.FrmSln == 0)
                return athDesc;

            //如果是自定义方案,就查询自定义方案信息.
            if (sln.FrmSln == 2)
            {
                BP.Sys.FrmAttachment athDescNode = new BP.Sys.FrmAttachment();
                athDescNode.setMyPK(this.FK_FrmAttachment + "_" + this.NodeID);
                if (athDescNode.RetrieveFromDBSources() == 0)
                {
                    //没有设定附件权限，保持原来的附件权限模式
                    return athDesc;
                }
                return athDescNode;
            }

            return null;
        }
        /// <summary>
        /// 生成描述
        /// </summary>
        /// <returns></returns>
        public BP.Sys.FrmAttachment GenerAthDesc()
        {
            #region 为累加表单做的特殊判断.
            if (this.GetRequestValInt("FormType") == 10)
            {
                if (this.FK_FrmAttachment.Contains(this.FrmID) == false)
                    return GenerAthDescOfFoolTruck(); //如果当前表单的ID。
            }
            #endregion

            BP.Sys.FrmAttachment athDesc = new BP.Sys.FrmAttachment();
            athDesc.setMyPK(this.FK_FrmAttachment);
            if (this.NodeID == 0 || this.FlowNo == null)
            {
                athDesc.RetrieveFromDBSources();
                return athDesc;
            }

            athDesc.setMyPK(this.FK_FrmAttachment);
            int result = athDesc.RetrieveFromDBSources();

            #region 判断是否是明细表的多附件.
            if (result == 0 && DataType.IsNullOrEmpty(this.FlowNo) == false
               && this.FK_FrmAttachment.Contains("AthMDtl"))
            {
                athDesc.FrmID = this.FrmID;
                athDesc.NoOfObj = "AthMDtl";
                athDesc.Name = "我的从表附件";
                athDesc.UploadType = AttachmentUploadType.Multi;
                athDesc.Insert();
            }
            #endregion 判断是否是明细表的多附件。

            #region 判断是否可以查询出来，如果查询不出来，就可能是公文流程。
            if (result == 0 && DataType.IsNullOrEmpty(this.FlowNo) == false
                && this.FK_FrmAttachment.Contains("DocMultiAth"))
            {
                /*如果没有查询到它,就有可能是公文多附件被删除了.*/
                athDesc.setMyPK(this.FK_FrmAttachment);
                athDesc.NoOfObj = "DocMultiAth";
                athDesc.FrmID = this.FrmID;
                athDesc.Exts = "*.*";

                //存储路径.
                // athDesc.SaveTo = "/DataUser/UploadFile/";
                athDesc.ItIsNote = false; //不显示note字段.
                athDesc.ItIsVisable = false; // 让其在form 上不可见.

                //位置.
                athDesc.H = (float)150;

                //多附件.
                athDesc.UploadType = AttachmentUploadType.Multi;
                athDesc.Name = "公文多附件(系统自动增加)";
                athDesc.SetValByKey("AtPara",
                    "@IsWoEnablePageset=1@IsWoEnablePrint=1@IsWoEnableViewModel=1@IsWoEnableReadonly=0@IsWoEnableSave=1@IsWoEnableWF=1@IsWoEnableProperty=1@IsWoEnableRevise=1@IsWoEnableIntoKeepMarkModel=1@FastKeyIsEnable=0@IsWoEnableViewKeepMark=1@FastKeyGenerRole=@IsWoEnableTemplete=1");
                athDesc.Insert();

                //有可能在其其它的节点上没有这个附件，所以也要循环增加上它.
                BP.WF.Nodes nds = new BP.WF.Nodes(this.FlowNo);
                foreach (BP.WF.Node nd in nds)
                {
                    athDesc.FrmID = "ND" + nd.NodeID;
                    athDesc.setMyPK(athDesc.FrmID + "_" + athDesc.NoOfObj);
                    if (athDesc.IsExits == true)
                        continue;

                    athDesc.Insert();
                }

                //重新查询一次，把默认值加上.
                athDesc.RetrieveFromDBSources();
            }
            #endregion 判断是否可以查询出来，如果查询不出来，就可能是公文流程。

            #region 处理权限方案。
            if (this.NodeID != 0)
            {
                string fk_mapdata = this.FrmID;
                if (this.FK_FrmAttachment.Contains("AthMDtl") == true)
                    fk_mapdata = this.GetRequestVal("FFK_MapData");

                if (DataType.IsNullOrEmpty(fk_mapdata))
                    fk_mapdata = this.GetRequestVal("FK_MapData");



                Node nd = new Node(this.NodeID);
                Flow flow = new Flow(nd.FlowNo);
                if (nd.HisFormType == NodeFormType.SheetTree || nd.HisFormType == NodeFormType.RefOneFrmTree || flow.FlowDevModel == FlowDevModel.JiJian)
                {
                    //如果是绑定表单树中的表单，重新赋值绑定表单的名字
                    if (nd.HisFormType == NodeFormType.RefOneFrmTree || flow.FlowDevModel == FlowDevModel.JiJian)
                    {
                        fk_mapdata = nd.NodeFrmID;
                    }
                    FrmNode fn = new FrmNode(nd.NodeID, fk_mapdata);
                    /*if (fn.FrmSln == FrmSln.Default)
                    {
                        if (fn.WhoIsPK == WhoIsPK.FID)
                            athDesc.HisCtrlWay = AthCtrlWay.FID;

                        if (fn.WhoIsPK == WhoIsPK.PWorkID)
                            athDesc.HisCtrlWay = AthCtrlWay.PWorkID;

                        if (fn.WhoIsPK == WhoIsPK.P2WorkID)
                            athDesc.HisCtrlWay = AthCtrlWay.P2WorkID;


                    }*/

                    if (fn.FrmSln == FrmSln.Readonly)
                    {

                        athDesc.HisDeleteWay = AthDeleteWay.None;
                        athDesc.ItIsUpload = false;
                        athDesc.ItIsDownload = true;
                        athDesc.setMyPK(this.FK_FrmAttachment);
                        return athDesc;
                    }

                    if (fn.FrmSln == FrmSln.Self)
                    {
                        if (this.FK_FrmAttachment.Contains("AthMDtl") == true)
                        {
                            athDesc.setMyPK(this.FrmID + "_" + nd.NodeID + "_AthMDtl");
                            athDesc.RetrieveFromDBSources();
                        }
                        else
                        {
                            athDesc.setMyPK(this.FK_FrmAttachment + "_" + nd.NodeID);
                            athDesc.RetrieveFromDBSources();
                        }
                        athDesc.setMyPK(this.FK_FrmAttachment);
                        return athDesc;
                    }
                }
            }
            #endregion 处理权限方案。

            return athDesc;
        }
        /// <summary>
        /// 打包下载.
        /// </summary>
        /// <returns></returns>
        public string AttachmentUpload_DownZip()
        {
            string zipName = this.WorkID + "_" + this.FK_FrmAttachment;

            #region 处理权限控制.
            BP.Sys.FrmAttachment athDesc = this.GenerAthDesc();

            //查询出来数据实体.
            BP.Sys.FrmAttachmentDBs dbs = BP.WF.CCFormAPI.GenerFrmAttachmentDBs(athDesc, this.PKVal, this.FK_FrmAttachment, this.WorkID, this.FID, this.PWorkID, true, this.NodeID, athDesc.FrmID);
            #endregion 处理权限控制.

            if (dbs.Count == 0)
                return "err@文件不存在，不需打包下载。";

            string basePath = BP.Difference.SystemConfig.PathOfDataUser + "Temp";
            string tempUserPath = basePath + "/" + WebUser.No;
            string tempFilePath = basePath + "/" + WebUser.No + "/" + this.OID;
            string zipPath = basePath + "/" + WebUser.No;
            string zipFile = zipPath + "/" + zipName + ".zip";

            string info = "";
            try
            {
                //删除临时文件，保证一个用户只能存一份，减少磁盘占用空间.
                info = "@创建用户临时目录:" + tempUserPath;
                if (System.IO.Directory.Exists(tempUserPath) == false)
                    System.IO.Directory.CreateDirectory(tempUserPath);

                //如果有这个临时的目录就把他删除掉.
                if (System.IO.Directory.Exists(tempFilePath) == true)
                    System.IO.Directory.Delete(tempFilePath, true);

                System.IO.Directory.CreateDirectory(tempFilePath);
            }
            catch (Exception ex)
            {
                return "err@组织临时目录出现错误:" + ex.Message;
            }

            try
            {
                foreach (FrmAttachmentDB db in dbs)
                {
                    string copyToPath = tempFilePath;

                    //求出文件路径.
                    string fileTempPath = db.GenerTempFile(athDesc.AthSaveWay);
                    string fileTempDecryPath = fileTempPath;
                    //获取文件是否加密
                    bool fileEncrypt = BP.Difference.SystemConfig.isEnableAthEncrypt;
                    bool isEncrypt = db.GetParaBoolen("IsEncrypt");
                    if (fileEncrypt == true && isEncrypt == true)
                    {
                        fileTempDecryPath = fileTempPath + ".tmp";
                        EncHelper.DecryptDES(fileTempPath, fileTempDecryPath);

                    }
                    if (DataType.IsNullOrEmpty(db.Sort) == false)
                    {
                        copyToPath = tempFilePath + "//" + db.Sort;
                        if (System.IO.Directory.Exists(copyToPath) == false)
                            System.IO.Directory.CreateDirectory(copyToPath);
                    }
                    //新文件目录
                    copyToPath = copyToPath + "//" + db.FileName;

                    if (File.Exists(fileTempDecryPath) == true)
                    {
                        File.Copy(fileTempDecryPath, copyToPath, true);
                    }
                    if (fileEncrypt == true && isEncrypt == true)
                        File.Delete(fileTempDecryPath);
                }
            }
            catch (Exception ex)
            {
                return "err@组织文件期间出现错误:" + ex.Message;
            }

            try
            {
                while (System.IO.File.Exists(zipFile) == true)
                {
                    System.IO.File.Delete(zipFile);
                }
                //执行压缩.
                FastZip fz = new FastZip();
                fz.CreateZip(zipFile, tempFilePath, true, "");
                //删除临时文件夹
                System.IO.Directory.Delete(tempFilePath, true);
            }
            catch (Exception ex)
            {
                return "err@执行压缩出现错误:" + ex.Message + ",路径tempPath:" + tempFilePath + ",zipFile=" + zipFile;
            }

            if (System.IO.File.Exists(zipFile) == false)
                return "err@压缩文件未生成成功,请在点击一次.";

            zipName = DataType.PraseStringToUrlFileName(zipName);

            GenerWorkerList gwf = new GenerWorkerList();
            gwf.Retrieve(GenerWorkerListAttr.FK_Emp, BP.Web.WebUser.No, GenerWorkerListAttr.FK_Node, this.NodeID, GenerWorkerListAttr.WorkID, this.WorkID);

            string str = gwf.GetParaString(athDesc.NoOfObj);
            str += "ALL";
            gwf.SetPara(athDesc.NoOfObj, str);
            gwf.Update();

            string url = HttpContextHelper.RequestApplicationPath + "DataUser/Temp/" + WebUser.No + "/" + zipName + ".zip";
            return "url@" + url;
        }

        public string AthSingle_Init()
        {
            string mypk = this.GetRequestVal("AthMyPK");
            if (DataType.IsNullOrEmpty(mypk) == true)
                return "err@请求参数MyPK的值不能为空";
            //PKVal
            string pkVal = DataType.IsNullOrEmpty(this.PKVal) == true ? null : this.PKVal;
            FrmAttachmentSingle ath = new FrmAttachmentSingle(mypk);
            //设计器中的预览。
            if (pkVal == null)
            {
                string file = BP.Difference.SystemConfig.PathOfTemp + "/" + mypk + ".docx";
                if (ath.AthSingleRole != 0)
                {
                    DBAccess.GetFileFromDB(file, ath.EnMap.PhysicsTable, "MyPK", mypk, "TemplateFile");
                    ath.SetPara("IsHaveFile", 0);
                }
                if (File.Exists(file) == false)
                    CreateDocFile(file);
                return ath.ToJson(false);
            }
            //获取athDB的数据
            FrmAttachmentDBs dbs = new FrmAttachmentDBs();
            dbs.Retrieve(FrmAttachmentDBAttr.FK_MapData, ath.FrmID, FrmAttachmentDBAttr.FK_FrmAttachment, ath.MyPK, FrmAttachmentDBAttr.RefPKVal, pkVal);
            FrmAttachmentDB db = null;
            string filePath = "";
            if (dbs.Count != 0)
            {
                db = dbs[0] as FrmAttachmentDB;
                filePath = db.FileFullName;
            }
            bool isReadonly = this.GetRequestValBoolen("IsReadonly");
            if (isReadonly == false && ath.AthSingleRole != 0 && File.Exists(filePath) == false)
            {
                if (System.IO.Directory.Exists(ath.SaveTo + pkVal) == false)
                    System.IO.Directory.CreateDirectory(ath.SaveTo + pkVal);
                string file = BP.Difference.SystemConfig.PathOfTemp + "/" + mypk + ".docx";
                DBAccess.GetFileFromDB(file, ath.EnMap.PhysicsTable, "MyPK", mypk, "TemplateFile");
                File.Copy(file, ath.SaveTo + pkVal + "/" + ath.MyPK + ".docx");
            }

            //判断公文文件是否存在
            if (File.Exists(db.FileFullName) == false)
                ath.SetPara("IsHaveFile", 0);
            else
                ath.SetPara("IsHaveFile", 1);
            return ath.ToJson(false);
        }

        public string AthSingle_Upload()
        {
            string mypk = this.GetRequestVal("AthMyPK");
            FrmAttachmentSingle ath = new FrmAttachmentSingle(mypk);
            FrmAttachmentDBs dbs = new FrmAttachmentDBs();
            string pkVal = DataType.IsNullOrEmpty(this.PKVal) == true ? null : this.PKVal;
            string fileName = mypk + ".docx";
            string filePath = ath.SaveTo + pkVal + "/" + fileName;
            if (HttpContextHelper.RequestFilesCount > 0)
            {
                //HttpPostedFile file = context.Request.Files[i];
                IFormFile file = HttpContextHelper.RequestFiles(0);
                //文件大小，单位字节
                int fileContentLength = (int)file.Length;
                //上传路径
                string savePath = ath.SaveTo + pkVal;
                //二进制数组
                byte[] fileBytes = null;
                fileBytes = new byte[fileContentLength];
                //创建Stream对象，并指向上传文件
                Stream fileStream = file.OpenReadStream();
                //从当前流中读取字节，读入字节数组中
                fileStream.Read(fileBytes, 0, fileContentLength);

                if (System.IO.Directory.Exists(savePath) == false)
                    System.IO.Directory.CreateDirectory(savePath);
                //创建文件，返回一个 FileStream，它提供对 path 中指定的文件的读/写访问。
                using (FileStream stream = File.Create(filePath))
                {
                    //将字节数组写入流
                    stream.Write(fileBytes, 0, fileBytes.Length);
                    stream.Close();
                }
                //HttpContextHelper.UploadFile(file, filePath);

            }
            dbs.Retrieve(FrmAttachmentDBAttr.FK_MapData, ath.FrmID, FrmAttachmentDBAttr.FK_FrmAttachment, ath.MyPK, FrmAttachmentDBAttr.RefPKVal, pkVal);
            if (dbs.Count == 0 && ath.AthEditModel != 0)
            {
                //增加一条数据
                FrmAttachmentDB db = new FrmAttachmentDB();
                db.setMyPK(DBAccess.GenerGUID());
                db.NodeID = this.NodeID;
                db.FrmID = ath.FrmID;
                db.FK_FrmAttachment = ath.MyPK;
                db.FID = this.FID; //流程id.
                db.RefPKVal = pkVal;
                db.FileExts = "docx";
                db.FileName = ath.MyPK + "." + db.FileExts;
                db.RDT = DataType.CurrentDateTimess;
                db.Rec = BP.Web.WebUser.No;
                db.RecName = BP.Web.WebUser.Name;
                db.DeptNo = WebUser.DeptNo;
                //设置路径.
                db.FileFullName = ath.SaveTo + db.RefPKVal + "/" + db.FileName;
                db.Insert();
            }
            return "";
        }

        public string AthSingle_TemplateData()
        {
            //获取表单数据
            string pkVal = DataType.IsNullOrEmpty(this.PKVal) == true ? null : this.PKVal;
            if (this.NodeID == 0)
                return "err@没有获取到FK_Node的值";
            if (pkVal == null)
                return "err@没有获取到OID的值";
            Node nd = new Node(this.NodeID);
            Work wk = nd.HisWork;
            wk.OID = Int64.Parse(pkVal);
            wk.RetrieveFromDBSources();
            Attrs attrs = wk.EnMap.Attrs;
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("text");
            dt.Columns.Add("type");
            DataRow dr = null;
            foreach (Attr attr in attrs)
            {
                dr = dt.NewRow();
                dr["name"] = attr.Key;
                dr["text"] = wk.GetValByKey(attr.Key);
                dr["type"] = "text";
                dt.Rows.Add(dr);
            }
            return BP.Tools.Json.ToJson(dt);

        }



        public string DearFileName(string fileName)
        {
            fileName = DearFileNameExt(fileName, "+", "%2B");
            fileName = DearFileNameExt(fileName, " ", "%20");
            fileName = DearFileNameExt(fileName, "/", "%2F");
            fileName = DearFileNameExt(fileName, "?", "%3F");
            fileName = DearFileNameExt(fileName, "%", "%25");
            fileName = DearFileNameExt(fileName, "#", "%23");
            fileName = DearFileNameExt(fileName, "&", "%26");
            fileName = DearFileNameExt(fileName, "=", "%3D");
            return fileName;
        }

        private void CreateDocFile(string filePath)
        {
            //Spire.Doc.Document doc = new Spire.Doc.Document();

            //doc.SaveToFile(filePath, Spire.Doc.FileFormat.Docx2013);
        }
        public string DearFileNameExt(string fileName, string val, string replVal)
        {
            fileName = fileName.Replace(val, replVal);
            fileName = fileName.Replace(val, replVal);
            fileName = fileName.Replace(val, replVal);
            fileName = fileName.Replace(val, replVal);
            fileName = fileName.Replace(val, replVal);
            fileName = fileName.Replace(val, replVal);
            fileName = fileName.Replace(val, replVal);
            fileName = fileName.Replace(val, replVal);
            return fileName;
        }
        #endregion 附件组件


        #region 必须传递参数
        /// <summary>
        /// 执行的内容
        /// </summary>
        public string DoWhat
        {
            get
            {
                string str = this.GetRequestVal("DoWhat");
                if (DataType.IsNullOrEmpty(str))
                    return "Frm";
                return str;
            }
        }
        /// <summary>
        /// 当前的用户
        /// </summary>
        public string UserNo
        {
            get
            {
                return this.GetRequestVal("UserNo");
            }
        }
        /// <summary>
        /// 用户的安全校验码(请参考集成章节)
        /// </summary>
        public string SID
        {
            get
            {
                return this.GetRequestVal("Token");
            }
        }
        public string AppPath
        {
            get
            {
                return BP.WF.Glo.CCFlowAppPath;
            }
        }

        public string Port_Init()
        {
            #region 安全性校验.
            if (this.UserNo == null || this.SID == null || this.DoWhat == null || this.FrmID == null)
            {
                return "err@必要的参数没有传入，请参考接口规则。";
            }

            if (BP.WF.Dev2Interface.Port_CheckUserLogin(this.UserNo, this.SID) == false)
            {
                return "err@非法的访问，请与管理员联系。SID=" + this.SID;
            }

            if (BP.Web.WebUser.No != this.UserNo)
            {
                BP.WF.Dev2Interface.Port_SigOut();
                BP.WF.Dev2Interface.Port_Login(this.UserNo);
            }

            #endregion 安全性校验.

            #region 生成参数串.
            string paras = "";
            foreach (string str in HttpContextHelper.RequestParamKeys)
            {
                string val = this.GetRequestVal(str);
                if (val.IndexOf('@') != -1)
                    throw new Exception("您没有能参数: [ " + str + " ," + val + " ] 给值 ，URL 将不能被执行。");
                switch (str.ToLower())
                {
                    case "fk_mapdata":
                    case "workid":
                    case "fk_node":
                    case "Token":
                        break;
                    default:
                        paras += "&" + str + "=" + val;
                        break;
                }
            }
            #endregion 生成参数串.

            string url = "";
            switch (this.DoWhat)
            {
                case "Frm": //如果是调用Frm的查看界面.
                    url = "Frm.htm?FK_MapData=" + this.FrmID + "&OID=" + this.OID + paras;
                    break;
                case "Search": //调用查询界面.
                    url = "../Comm/Search.htm?EnsName=" + this.FrmID + paras;
                    break;
                case "Group": //分组分析界面.
                    url = "../Comm/Group.htm?EnsName=" + this.FrmID + paras;
                    break;
                default:
                    break;
            }
            return "url@" + url;
        }
        #endregion


        #region 前台SQL转移处理
        public string RunSQL_Init()
        {
            string sql = GetRequestVal("SQL");
            DBAccess.RunSQLReturnTable(sql);
            DataTable dt = new DataTable();
            return BP.Tools.Json.ToJson(dt);
        }
        #endregion


        #region 表单数据版本比对

        public string FrmDB_DoCompare()
        {
            FrmDBVer ver = new FrmDBVer(this.MyPK);
            string mainData = BP.DA.DBAccess.GetBigTextFromDB("Sys_FrmDBVer", "MyPK", this.MyPK, "FrmDB");
            DataTable dt = BP.Tools.Json.ToDataTable(mainData);
            dt.TableName = "mainData";
            MapDtls dtls = new MapDtls(ver.FrmID);
            MapData md = new MapData(ver.FrmID);
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ds.Tables.Add(ver.ToDataTableField("Sys_FrmDBVer"));
            ds.Tables.Add(dtls.ToDataTableField("Sys_MapDtl"));
            ds.Tables.Add(md.ToDataTableField("Sys_MapData"));
            return BP.Tools.Json.ToJson(ds);
        }

        public string FrmDBVerAndRemark_Init()
        {
            DataSet ds = new DataSet();
            string field = GetRequestVal("Field");
            int fieldType = GetRequestValInt("FieldType"); // 0 表单字段 1从表
            string frmID = this.FrmID;
            string rfrmID = this.GetRequestVal("RFrmID");
            //文本字段
            if (fieldType == 0)
            {
                FrmDBRemarks remarks = new FrmDBRemarks();
                remarks.Retrieve(FrmDBRemarkAttr.FrmID, rfrmID, FrmDBRemarkAttr.Field, field, FrmDBRemarkAttr.RefPKVal, this.RefPKVal, FrmDBRemarkAttr.RDT);
                ds.Tables.Add(remarks.ToDataTableField("Sys_FrmDBRemark"));
                FrmDBVers vers = new FrmDBVers();
                vers.Retrieve(FrmDBVerAttr.FrmID, rfrmID, FrmDBVerAttr.RefPKVal, this.RefPKVal, FrmDBVerAttr.RDT);

                foreach (FrmDBVer ver in vers)
                {
                    string json = DBAccess.GetBigTextFromDB("Sys_FrmDBVer", "MyPK", ver.MyPK, "FrmDB");
                    if (DataType.IsNullOrEmpty(json) == true)
                    {
                        ver.TrackID = "";
                        continue;
                    }

                    DataTable dt = BP.Tools.Json.ToDataTable(json);
                    ver.TrackID = dt.Rows[0][field].ToString();
                }

                ds.Tables.Add(vers.ToDataTableField("Sys_FrmDBVer"));
                return BP.Tools.Json.ToJson(ds);
            }

            return FrmDB_DtlCompare(field, rfrmID);
            //return BP.Tools.Json.ToJson(ds);
        }

        /// <summary>
        /// 从表数据的比对
        /// </summary>
        /// <returns></returns>
        public string FrmDB_DtlCompare(string mapDtl, string rfrmID)
        {
            DataSet myds = new DataSet();
            #region 加载从表表单模版信息.
            MapDtl dtl = new MapDtl(mapDtl);
            DataTable Sys_MapDtl = dtl.ToDataTableField("Sys_MapDtl");
            myds.Tables.Add(Sys_MapDtl);

            //明细表的表单描述
            MapAttrs attrs = dtl.MapAttrs;
            DataTable Sys_MapAttr = attrs.ToDataTableField("Sys_MapAttr");
            myds.Tables.Add(Sys_MapAttr);

            //明细表的配置信息.
            DataTable Sys_MapExt = dtl.MapExts.ToDataTableField("Sys_MapExt");
            myds.Tables.Add(Sys_MapExt);

            //启用附件，增加附件信息
            DataTable Sys_FrmAttachment = dtl.FrmAttachments.ToDataTableField("Sys_FrmAttachment");
            myds.Tables.Add(Sys_FrmAttachment);
            #endregion 加载从表表单模版信息.

            #region  把从表的数据放入.
            FrmDBVers vers = new FrmDBVers();
            vers.Retrieve(FrmDBVerAttr.FrmID, rfrmID, FrmDBVerAttr.RefPKVal, this.RefPKVal, FrmDBVerAttr.RDT);

            foreach (FrmDBVer ver in vers)
            {
                string json = DBAccess.GetBigTextFromDB("Sys_FrmDBVer", "MyPK", ver.MyPK, "FrmDtlDB");
                ver.TrackID = json;
            }
            myds.Tables.Add(vers.ToDataTableField("Sys_FrmDBVer"));

            #endregion  把从表的数据放入.
            return BP.Tools.Json.ToJson(myds);
        }
        #endregion 表单数据版本比对

        #region 章节表单.
        public string ChapterFrm_Init()
        {
            MapData md = new MapData(this.FrmID);
            DataSet ds = BP.Sys.CCFormAPI.GenerHisDataSet(this.FrmID);
            GEEntityOID en = new GEEntityOID(this.FrmID);
            en.OID = this.OID;
            if (en.OID == 0)
                en.ResetDefaultVal();
            if (en.RetrieveFromDBSources() == 0)
                en.InsertAsOID(this.OID);

            GroupFields gfs = md.GroupFields;

            MapAttrs attrs = md.MapAttrs;


            //获取外键下拉
            DataTable ddlTable = new DataTable();
            ddlTable.Columns.Add("No");
            MapExts mes = md.MapExts;
            MapExt me = null;
            foreach (MapAttr attr in attrs)
            {
                FieldTypeS lgType = attr.LGType;
                string uiBindKey = attr.UIBindKey;

                if (DataType.IsNullOrEmpty(uiBindKey) == true)
                    continue; //为空就continue.

                if (lgType == FieldTypeS.Enum)
                    continue; //枚举值就continue;
                if (attr.UIVisible == false)
                    continue;

                bool uiIsEnable = attr.UIIsEnable;
                if (uiIsEnable == false && lgType == FieldTypeS.Enum)
                    continue; //如果是外键，并且是不可以编辑的状态.

                if (uiIsEnable == false && lgType == FieldTypeS.Normal)
                    continue; //如果是外部数据源，并且是不可以编辑的状态.

                // 检查是否有下拉框自动填充。
                string keyOfEn = attr.KeyOfEn;
                string fk_mapData = attr.FrmID;

                #region 处理下拉框数据范围. for 小杨.
                me = mes.GetEntityByKey(MapExtAttr.ExtType, MapExtXmlList.AutoFullDLL, MapExtAttr.AttrOfOper, keyOfEn) as MapExt;
                // 过滤掉空sql
                if (me != null && !DataType.IsNullOrEmpty(me.Doc))
                {
                    string fullSQL = me.Doc.Clone() as string;
                    fullSQL = fullSQL.Replace("~", ",");
                    fullSQL = BP.WF.Glo.DealExp(fullSQL, en, null);
                    DataTable dt = DBAccess.RunSQLReturnTable(fullSQL);
                    if (BP.Difference.SystemConfig.AppCenterDBFieldCaseModel != FieldCaseModel.None)
                    {
                        string columnName = "";
                        foreach (DataColumn col in dt.Columns)
                        {
                            columnName = col.ColumnName.ToUpper();
                            switch (columnName)
                            {
                                case "NO":
                                    col.ColumnName = "No";
                                    break;
                                case "NAME":
                                    col.ColumnName = "Name";
                                    break;
                                case "PARENTNO":
                                    col.ColumnName = "ParentNo";
                                    break;
                            }
                        }
                    }

                    dt.TableName = keyOfEn; //可能存在隐患，如果多个字段，绑定同一个表，就存在这样的问题.
                    ds.Tables.Add(dt);
                    continue;
                }
                #endregion 处理下拉框数据范围.

                // 判断是否存在.
                if (ds.Tables.Contains(uiBindKey) == true)
                    continue;

                DataTable dataTable = BP.Pub.PubClass.GetDataTableByUIBineKey(uiBindKey);

                if (dataTable != null)
                    ds.Tables.Add(dataTable);
                else
                {
                    DataRow ddldr = ddlTable.NewRow();
                    ddldr["No"] = uiBindKey;
                    ddlTable.Rows.Add(ddldr);
                }
            }
            ddlTable.TableName = "UIBindKey";
            ds.Tables.Add(ddlTable);
            #region 获得数据，是否打勾？
            //获得已经有数据的字段.
            string ptable = en.EnMap.PhysicsTable;
            string atparas = DBAccess.RunSQLReturnString("SELECT AtPara FROM " + ptable + " WHERE OID=" + this.OID.ToString());
            AtPara ap = new AtPara(atparas);
            string fileds = ap.GetValStrByKey("ChapterFrmSaveFields");
            if (DataType.IsNullOrEmpty(fileds) == false)
            {
                //增加星号标记.
                foreach (MapAttr attr in attrs)
                {
                    if (fileds.Contains("," + attr.KeyOfEn + ",") == true)
                    {
                        attr.SetPara("IsStar", "1");
                    }
                }

                //为分组字段设置 IsStar 标记.  标记该分组下，是否所有的字段都已经填写完毕?
                foreach (GroupField gf in gfs)
                {
                    //章节表单Attr
                    if (gf.CtrlType.Equals("Attr") == true)
                    {
                        MapAttrs newAttrs = attrs.GetEntitiesByKey("GroupID", gf.OID.ToString()) as MapAttrs;
                        int blankNum = 0;
                        if (newAttrs != null)
                        {
                            foreach (MapAttr item in newAttrs)
                            {
                                if (item.UIIsInput == true)
                                {
                                    string val = en.GetValStrByKey(item.KeyOfEn);
                                    if (DataType.IsNullOrEmpty(val) == true)
                                    {
                                        blankNum++;
                                    }
                                }
                            }
                        }

                        if (blankNum == 0)
                        {
                            gf.SetPara("IsStar", "1");
                            continue;
                        }
                    }

                    //判断是否是从表? 取从表的数量.
                    if (gf.CtrlType.Equals("Dtl") == true)
                    {
                        GEEntity geen = new GEEntity(gf.CtrlID);
                        try
                        {
                            string sql = "SELECT count(*) as num FROM " + geen.EnMap.PhysicsTable + " WHERE refpk='" + this.WorkID + "'";
                            if (DBAccess.RunSQLReturnValInt(sql) > 0)
                            {
                                gf.SetPara("IsStar", "1");
                                continue;
                            }
                        }
                        catch (Exception ex)
                        {
                            geen.CheckPhysicsTable();
                            gf.SetPara("IsStar", "0");
                            continue;
                        }
                    }

                    //是否是附件？
                    if (gf.CtrlType.Equals("Ath") == true)
                    {
                        string sql = "SELECT COUNT(*) AS NUM FROM Sys_FrmattachmentDB WHERE RefPKVal=" + this.WorkID + " ";
                        if (DBAccess.RunSQLReturnValInt(sql) > 0)
                        {
                            gf.SetPara("IsStar", "1");
                            continue;
                        }
                    }

                    //表单链接, 检查是否有空白项？
                    if (gf.CtrlType.Equals("ChapterFrmLinkFrm") == true)
                    {
                        GEEntityOID ge = new GEEntityOID(gf.CtrlID);
                        ge.OID = this.WorkID;
                        if (ge.RetrieveFromDBSources() == 1)
                        {
                            int blankNum = 0;
                            MapAttrs attr1s = new MapAttrs(gf.CtrlID);
                            foreach (MapAttr item in attr1s)
                            {
                                if (item.UIIsInput == true)
                                {
                                    string val = ge.GetValStrByKey(item.KeyOfEn);
                                    if (DataType.IsNullOrEmpty(val) == true)
                                    {
                                        blankNum++;
                                    }
                                }
                            }
                            if (blankNum == 0)
                            {
                                gf.SetPara("IsStar", "1");
                                continue;
                            }
                        }
                    }



                    //判断字段.
                    if (DataType.IsNullOrEmpty(gf.CtrlType) == false)
                        continue;

                    //是否有未填写的字段？
                    bool isHaveBlank = false;
                    foreach (MapAttr attr in attrs)
                    {
                        if (attr.GroupID != gf.OID)
                            continue;
                        if (attr.MyDataType != DataType.AppString || attr.UIVisible == false)
                            continue;
                        //if (attr.MaxLen < 2000)
                        //    continue;
                        if (fileds.Contains(attr.KeyOfEn + ",") == false)
                        {
                            isHaveBlank = true;
                        }
                    }
                    if (isHaveBlank == false)
                        gf.SetPara("IsStar", "1");
                }
            }
            #endregion 获得数据，是否打勾？

            //组装数据，返回出去.

            //ds.Tables.Add(gfs.ToDataTableField("GroupFields"));
            DataTable mainTable = en.ToDataTableField("MainTable");
            ds.Tables.Add(mainTable);
            ds.Tables.Remove("Sys_GroupField");
            ds.Tables.Remove("Sys_MapAttr");
            ds.Tables.Add(attrs.ToDataTableField("Sys_MapAttr"));
            ds.Tables.Add(gfs.ToDataTableField("GroupFields"));
            GenerWorkFlow gwf = new GenerWorkFlow(this.OID);
            ds.Tables.Add(gwf.ToDataTableField("WF_GenerWorkFlow"));
            // ds.WriteXml("d:\\xxx.xml");

            return BP.Tools.Json.ToJson(ds);
        }

        public string ChapterFrm_AttrInit()
        {
            GEEntityOID en = new GEEntityOID(this.FrmID);
            en.OID = this.OID;
            en.Retrieve();
            return en.ToJson();
        }
        /// <summary>
        /// 检查目录是否可以加星.
        /// </summary>
        /// <returns></returns>
        public string ChapterFrm_CheckGroupFieldStar()
        {
            GroupField gf = new GroupField(this.GroupField);

            //判断是否是从表? 取从表的数量.
            if (gf.CtrlType.Equals("Dtl") == true)
            {
                GEEntity geen = new GEEntity(gf.CtrlID);
                string sql = "SELECT count(*) as num FROM " + geen.EnMap.PhysicsTable + " WHERE refpk='" + this.WorkID + "'";
                if (DBAccess.RunSQLReturnValInt(sql) > 0)
                    return "1";
                return "0";
            }

            //是否是附件？
            if (gf.CtrlType.Equals("Ath") == true)
            {
                string sql = "SELECT COUNT(*) AS NUM FROM Sys_FrmattachmentDB WHERE RefPKVal=" + this.WorkID + " ";
                if (DBAccess.RunSQLReturnValInt(sql) > 0)
                    return "1";
                return "0";
            }

            //表单链接, 检查是否有空白项？
            if (gf.CtrlType.Equals("ChapterFrmLinkFrm") == true)
            {
                GEEntityOID ge = new GEEntityOID(gf.CtrlID);
                ge.OID = this.WorkID;
                if (ge.RetrieveFromDBSources() == 1)
                {
                    int blankNum = 0;
                    MapAttrs attr1s = new MapAttrs(gf.CtrlID);
                    foreach (MapAttr item in attr1s)
                    {
                        if (item.UIIsInput == true)
                        {
                            string val = ge.GetValStrByKey(item.KeyOfEn);
                            if (DataType.IsNullOrEmpty(val) == true)
                                blankNum++;
                        }
                    }
                    if (blankNum == 0)
                        return "1";
                }
                return "0";
            }
            //章节表单Attr
            if (gf.CtrlType.Equals("Attr") == true)
            {
                GEEntityOID ge = new GEEntityOID(gf.CtrlID);
                ge.OID = this.WorkID;
                if (ge.RetrieveFromDBSources() == 1)
                {
                    int blankNum = 0;
                    MapAttrs attr1s = new MapAttrs();
                    attr1s.Retrieve(MapAttrAttr.FK_MapData, gf.CtrlID, MapAttrAttr.GroupID, gf.OID);
                    foreach (MapAttr item in attr1s)
                    {
                        if (item.UIIsInput == true)
                        {
                            string val = ge.GetValStrByKey(item.KeyOfEn);
                            if (DataType.IsNullOrEmpty(val) == true)
                                blankNum++;
                        }
                    }
                    if (blankNum == 0)
                        return "1";
                }
                return "0";
            }
            return "err@没有判断." + gf.ToJson();
        }


        public string ChapterFrm_InitOneField()
        {
            if (BP.DA.DataType.IsNullOrEmpty(this.KeyOfEn) == true)
                return "err@没有传来字段KeyOfEn的值.";

            string ptable = DBAccess.RunSQLReturnString("SELECT PTable FROM Sys_MapData WHERE No='" + this.FrmID + "'", null);
            if (SystemConfig.AppCenterDBType == DBType.Oracle || BP.Difference.SystemConfig.AppCenterDBType == DBType.GBASE8CByOracle)
                return BP.DA.DBAccess.RunSQLReturnStringIsNull("SELECT " + this.KeyOfEn + " From " + ptable + " WHERE OID=" + this.OID, "");

            return BP.DA.DBAccess.GetBigTextFromDB(ptable, "OID", this.OID.ToString(), this.KeyOfEn);
        }
        public string ChapterFrm_SaveOneField()
        {
            string ptable = DBAccess.RunSQLReturnString("SELECT PTable FROM Sys_MapData WHERE No='" + this.FrmID + "'", null);
            string vals = this.Vals;
            if (vals == null)
                vals = "";

            try
            {
                if (SystemConfig.AppCenterDBType == DBType.Oracle || BP.Difference.SystemConfig.AppCenterDBType == DBType.GBASE8CByOracle)
                {
                    try
                    {
                        Paras ps = new Paras();
                        ps.SQL = "UPDATE " + ptable + " SET " + this.KeyOfEn + "=" + SystemConfig.AppCenterDBVarStr + "KeyOfEn WHERE OID=" + this.OID;
                        ps.Add("KeyOfEn", vals);
                        BP.DA.DBAccess.RunSQL(ps);
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains("的值太大") == true)
                        {
                            //更改当前字段的长度
                            DBAccess.RunSQL("ALTER table " + ptable + " modify " + this.KeyOfEn + " VARCHAR2(4000)");
                            Paras ps = new Paras();
                            ps.SQL = "UPDATE " + ptable + " SET " + this.KeyOfEn + "=" + SystemConfig.AppCenterDBVarStr + "KeyOfEn WHERE OID=" + this.OID;
                            ps.Add("KeyOfEn", vals);
                            BP.DA.DBAccess.RunSQL(ps);
                        }
                    }

                }
                else
                {
                    BP.DA.DBAccess.SaveBigTextToDB(vals, ptable, "OID", this.OID.ToString(), this.KeyOfEn);
                }
                string atparas = DBAccess.RunSQLReturnStringIsNull("SELECT AtPara FROM " + ptable + " WHERE OID=" + this.OID.ToString(), "");

                //标记该字段已经完成.
                if (atparas.Contains("," + this.KeyOfEn + ",") == false)
                {
                    AtPara ap = new AtPara(atparas);
                    string val = ap.GetValStrByKey("ChapterFrmSaveFields");

                    if (vals.Length == 0)
                        val = val.Replace("," + this.KeyOfEn + ",", "");
                    else
                        val += "," + this.KeyOfEn + ",";

                    ap.SetVal("ChapterFrmSaveFields", val);

                    string strs = ap.GenerAtParaStrs();
                    int i = DBAccess.RunSQL("UPDATE " + ptable + " SET AtPara='" + strs + "' WHERE OID=" + this.OID);
                    if (i == 0)
                        return "err@保存章节字段出现错误，行数据OID=" + this.OID + ",不存在.";

                    if (this.Vals == null || this.Vals.Length == 0)
                        return "0"; //去掉star.
                    return "1";
                }
                else
                {
                    //如果存在，并且值为null. qudiao.
                    if (vals == "")
                    {
                        atparas = atparas.Replace("," + this.KeyOfEn + ",", "");
                        int i = DBAccess.RunSQL("UPDATE " + ptable + " SET AtPara='" + atparas + "' WHERE OID=" + this.OID);
                        if (i == 0)
                            return "err@保存章节字段出现错误，行数据OID=" + this.OID + ",不存在.";
                        return "0";
                    }
                }
                return "1"; // 增加star.
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("ta too long for column") == true || ex.Message.Contains("太长") == true)
                {
                    BP.DA.DBAccess.DropTableColumn(this.FrmID, this.KeyOfEn);
                    BP.DA.DBAccess.SaveBigTextToDB(this.Vals, this.FrmID, "OID", this.OID.ToString(), this.KeyOfEn);
                    return "保存成功.";
                }
                return ex.Message;
            }
        }

        public string ChapterFrm_SaveAttr()
        {
            //获取表单数据
            Hashtable ht = BP.Pub.PubClass.GetMainTableHT();
            MapAttrs attrs = new MapAttrs();
            attrs.Retrieve(MapAttrAttr.FK_MapData, this.FrmID, MapAttrAttr.GroupID, this.GetRequestValInt("GroupID"));

            GEEntity ge = new GEEntity(this.FrmID, this.PKVal);
            foreach (MapAttr attr in attrs)
            {
                string val = ht[attr.KeyOfEn] == null ? "" : ht[attr.KeyOfEn].ToString();
                ge.SetValByKey(attr.KeyOfEn, val);
            }
            ge.Update();
            return "保存成功";
        }
        /// <summary>
        /// 根据版本号获取表单的历史数据
        /// </summary>
        /// <returns></returns>
        public string ChartFrm_GetBigTextByVer()
        {
            string dbstr = SystemConfig.AppCenterDBVarStr;
            string sql = "SELECT MyPK FROM Sys_FrmDBVer WHERE FrmID=" + dbstr + "FrmID AND RefPKVal=" + dbstr + "RefPKVal AND Ver=" + dbstr + "Ver";
            Paras ps = new Paras();
            ps.SQL = sql;
            ps.Add("FrmID", this.FrmID);
            ps.Add("RefPKVal", this.OID);
            ps.Add("Ver", GetRequestVal("DBVer"));
            string mypk = DBAccess.RunSQLReturnStringIsNull(ps, "");
            if (DataType.IsNullOrEmpty(mypk) == true)
                return "err@获取表单数据版本失败";
            return DBAccess.GetBigTextFromDB("Sys_FrmDBVer", "MyPK", mypk, "FrmDB");
        }

        public string ChartFrm_GetDtlDataByVer()
        {
            return DBAccess.GetBigTextFromDB("Sys_FrmDBVer", "MyPK", this.MyPK, "FrmDtlDB");
        }
        #endregion 章节表单.
        public string Search_ExpByRTFOrExcel()
        {
            string ensName = this.EnsName;
            MapDtl mapDtl = new MapDtl(ensName);
            int isExp = mapDtl.GetValIntByKey("IsImp");
            string frmID = mapDtl.FrmID;
            string dtlRefPKVal = this.RefPKVal; //从表的RefPK
            if (this.NodeID != 0)
            {
                frmID = frmID.Replace("_" + this.NodeID, "");
                Node nd = new Node(this.NodeID);
                FrmNode fn = new FrmNode(nd.NodeID, frmID);
                if (fn.FrmSln == FrmSln.Self)
                {
                    mapDtl.No = this.EnsName + "_" + this.NodeID;
                    if (mapDtl.RetrieveFromDBSources() == 0)
                    {
                        mapDtl.No = this.EnsName;
                        mapDtl.RetrieveFromDBSources();
                    }
                }
                dtlRefPKVal = Dev2Interface.GetDtlRefPKVal(this.WorkID, this.PWorkID, this.FID, this.NodeID, frmID, mapDtl);
                if (dtlRefPKVal.Equals("0") == true || DataType.IsNullOrEmpty(dtlRefPKVal))
                {
                    dtlRefPKVal = this.RefPKVal;
                }
            }
            //实体.
            Entity en = null;
            MapData mapData = new MapData(frmID);
            if (mapData.EntityType == EntityType.FrmEntityNoName)
            {
                en = new GEEntityNoName(frmID);
                en.SetValByKey("No", this.RefPKVal);
                if (en.RetrieveFromDBSources() == 0)
                {
                    en.SetValByKey("No", dtlRefPKVal);
                    en.RetrieveFromDBSources();
                }
            }
            else
            {
                en = new GEEntity(frmID);
                en.SetValByKey("OID", Int64.Parse(this.RefPKVal));
                if (en.RetrieveFromDBSources() == 0)
                {
                    en.SetValByKey("OID", Int64.Parse(dtlRefPKVal));
                    en.RetrieveFromDBSources();
                }
            }
            string orderField = this.GetRequestVal("OrderField");
            string orderBy = this.GetRequestVal("OrderBy");
            GEDtls dtls = new GEDtls(mapDtl.No);
            QueryObject qo = BP.WF.CCFormAPI.GetDtls(dtls, mapDtl, en, dtlRefPKVal, this.PageIdx, this.GetRequestVal("paras"), orderField, orderBy);
            qo.DoQuery();
            //rtf模式导出
            string dirName = WebUser.No + this.EnsName.Replace(".", "");
            if (isExp == 2)
            {
                //读取文件rtf文件
                string filePath = SystemConfig.PathOfDataUser + "/TempleteOfExp/" + this.EnsName + ".rtf";
                string templateFile = SystemConfig.PathOfTemp + "/" + dirName;
                if (File.Exists(templateFile) == false)
                    File.Create(templateFile);
                templateFile += "/" + mapDtl.Name + ".doc";
                RTFEngine rtfEngine = new RTFEngine();
                rtfEngine.TempFilePath = templateFile;
                rtfEngine.MakeDoc(filePath, this.EnsName, dtls);
                return "DataUser/Temp/" + dirName + "/" + mapDtl.Name + ".doc";
            }
            // excel模式导出
            if (isExp == 3)
            {
                string filePath = SystemConfig.PathOfDataUser + "/TempleteOfExp/" + this.EnsName + ".xlsx";
                string templateFile = SystemConfig.PathOfTemp + "/" + dirName;
                if (File.Exists(templateFile) == false)
                    File.Create(templateFile);
                templateFile += "/" + mapDtl.Name + ".xlsx";
                if (File.Exists(templateFile) == false)
                    File.Delete(templateFile);
                File.Copy(filePath, templateFile);
                BP.Tools.ExportExcelUtil.ExportDGToExcel(templateFile, dtls);
                return "DataUser/Temp/" + dirName + "/" + mapDtl.Name + ".xlsx";


            }
            return "";

        }

    }
}

